Index: trunk/zoo-services/ogr/base-vect-ops/service.c
===================================================================
--- trunk/zoo-services/ogr/base-vect-ops/service.c	(revision 1)
+++ trunk/zoo-services/ogr/base-vect-ops/service.c	(revision 9)
@@ -23,6 +23,8 @@
  */
 
-#include "cpl_minixml.h"
+#include "cpl_conv.h"
 #include "ogr_api.h"
+#include "ogr_geometry.h"
+#include "geos_c.h"
 #include "service.h"
 
@@ -39,5 +41,4 @@
 #include <openssl/buffer.h>
 
-  xmlNodeSet* extractFromDoc(xmlDocPtr,char*);
   void printExceptionReportResponse(maps*,map*);
   char *base64(const unsigned char *input, int length);
@@ -71,9 +72,12 @@
     }
     xmlDocDumpFormatMemory(ndoc, &xmlbuff, &buffersize, 1);
-    char *tmp=strdup(strstr((char*)xmlbuff,"?>")+2);
+    char *tmp=(char*)calloc((xmlStrlen(xmlStrstr(xmlbuff,BAD_CAST "?>"))-1),sizeof(char));
+    sprintf(tmp,"%s",xmlStrstr(xmlbuff,BAD_CAST "?>")+2);
+    //strdup(strstr((char*)xmlbuff,"?>")+2);
     xmlXPathFreeObject(xpathObj);
-    xmlXPathFreeContext(xpathCtx); 
+    xmlXPathFreeContext(xpathCtx);
     xmlFree(xmlbuff);
     xmlFreeDoc(doc);
+    xmlFreeDoc(ndoc);
     xmlCleanupParser();
 #ifdef DEBUG
@@ -81,12 +85,301 @@
 #endif
     OGRGeometryH res=OGR_G_CreateFromGML(tmp);
+    free(tmp);
     if(res==NULL){
-    	map* tmp=createMap("text","Unable to call OGR_G_CreatFromGML");
-	addToMap(tmp,"code","NoApplicableCode");
-	printExceptionReportResponse(conf,tmp);
-	exit(0);
+      map* tmp=createMap("text","Unable to call OGR_G_CreatFromGML");
+      addToMap(tmp,"code","NoApplicableCode");
+      printExceptionReportResponse(conf,tmp);
+      exit(0);
     }
     else
-    	return OGR_G_CreateFromGML(tmp);
+      return res;//OGR_G_CreateFromGML(tmp);
+  }
+
+  int Simplify(maps*& conf,maps*& inputs,maps*& outputs){
+    maps* cursor=inputs;
+    OGRGeometryH geometry,res;
+    double tolerance;
+    map* tmp0=getMapFromMaps(cursor,"Tolerance","value");
+    if(tmp0==NULL){
+      tolerance=atof("2.0");
+    }
+    else
+      tolerance=atof(tmp0->value);
+    fprintf(stderr,"Tolerance for Simplify %f",tolerance);
+    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
+    if(!tmp)
+      return SERVICE_FAILED;
+    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeTime");
+    if(tmp1!=NULL){
+      if(strncmp(tmp1->value,"text/js",7)==0 ||
+	 strncmp(tmp1->value,"application/json",7)==0)
+        geometry=OGR_G_CreateGeometryFromJson(tmp->value);
+      else
+        geometry=createGeometryFromGML(conf,tmp->value);
+    }
+    else
+      geometry=createGeometryFromGML(conf,tmp->value);
+    GEOSGeometry* ggeometry=((OGRGeometry *) geometry)->exportToGEOS();
+    GEOSGeometry* gres=GEOSTopologyPreserveSimplify(ggeometry,tolerance);
+    res=OGRGeometryFactory::createFromGEOS(gres);
+    if(tmp1!=NULL){
+      if(strncmp(tmp1->value,"text/js",7)==0 ||
+	 strncmp(tmp1->value,"application/json",16)==0){
+        outputs->content=createMap("value",OGR_G_ExportToJson(tmp->value));
+	addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
+	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
+      }
+      else{
+        outputs->content=createMap("value",OGR_G_ExportToGML(res));
+	addMapToMap(&outputs->content,createMap("mimeType","text/xml"));
+	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
+	addMapToMap(&outputs->content,createMap("schema","http://fooa/gml/3.1.0/polygon.xsd"));
+      }
+    }else{
+      outputs->content=createMap("value",OGR_G_ExportToJson(res));
+      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
+      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
+    }
+    outputs->next=NULL;
+    //GEOSFree(ggeometry);
+    //GEOSFree(gres);
+    OGR_G_DestroyGeometry(res);
+    OGR_G_DestroyGeometry(geometry);
+    return SERVICE_SUCCEEDED;
+  }
+
+
+  int applyOne(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometryH (*myFunc)(OGRGeometryH)){
+#ifdef DEBUG
+    fprintf(stderr,"\nService internal print\n");
+#endif
+    maps* cursor=inputs;
+    OGRGeometryH geometry,res;
+#ifdef DEBUG
+    dumpMaps(cursor);
+#endif
+    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
+    if(!tmp)
+      return SERVICE_FAILED;
+    fprintf(stderr,"Service internal print \n");
+    dumpMaps(inputs);
+    fprintf(stderr,"/Service internal print \n");
+    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
+    fprintf(stderr,"Service internal print \n");
+    dumpMap(tmp1);
+    fprintf(stderr,"/Service internal print \n");
+    if(tmp1!=NULL){
+      if(strncmp(tmp1->value,"text/js",7)==0 ||
+	 strncmp(tmp1->value,"application/json",7)==0)
+        geometry=OGR_G_CreateGeometryFromJson(tmp->value);
+      else
+        geometry=createGeometryFromGML(conf,tmp->value);
+    }
+    else
+      geometry=createGeometryFromGML(conf,tmp->value);
+    res=(*myFunc)(geometry);
+    fprintf(stderr,"Service internal print \n");
+    dumpMaps(outputs);
+    fprintf(stderr,"/Service internal print \n");
+    map *tmp_2=getMapFromMaps(outputs,"Result","mimeType");
+    fprintf(stderr,"Service internal print \n");
+    dumpMap(tmp_2);
+    fprintf(stderr,"/Service internal print \n");
+    if(tmp_2!=NULL){
+      if(strncmp(tmp_2->value,"text/js",7)==0 ||
+	 strncmp(tmp_2->value,"application/json",16)==0){
+	char *tres=OGR_G_ExportToJson(res);
+        addToMap(outputs->content,"value",tres);
+	free(tres);
+	addToMap(outputs->content,"mimeType","text/plain");
+	addToMap(outputs->content,"encoding","UTF-8");
+      }
+      else{
+	char *tres=OGR_G_ExportToGML(res);
+        addToMap(outputs->content,"value",tres);
+	free(tres);
+      }
+    }else{
+      char *tres=OGR_G_ExportToJson(res);
+      addToMap(outputs->content,"value",tres);
+      free(tres);
+      addToMap(outputs->content,"mimeType","text/plain");
+      addToMap(outputs->content,"encoding","UTF-8");
+    }
+    outputs->next=NULL;
+#ifdef DEBUG
+    dumpMaps(outputs);
+    fprintf(stderr,"\nService internal print\n===\n");
+#endif
+    OGR_G_DestroyGeometry(res);
+    OGR_G_DestroyGeometry(geometry);
+    //CPLFree(res);
+    //CPLFree(geometry);
+    fprintf(stderr,"Service internal print \n");
+    dumpMaps(outputs);
+    fprintf(stderr,"/Service internal print \n");
+    return SERVICE_SUCCEEDED;
+  }
+
+#ifdef WIN32
+  __declspec(dllexport)
+#endif
+int Buffer(maps*& conf,maps*& inputs,maps*& outputs){
+   OGRGeometryH geometry,res;
+   map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
+   if(tmp==NULL)
+     return SERVICE_FAILED;
+   map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
+   if(strncmp(tmp1->value,"application/json",16)==0)
+     geometry=OGR_G_CreateGeometryFromJson(tmp->value);
+   else
+     geometry=createGeometryFromGML(conf,tmp->value);
+   int bufferDistance=1;
+   tmp=getMapFromMaps(inputs,"BufferDistance","value");
+   if(tmp!=NULL)
+		bufferDistance=atoi(tmp->value);
+   res=OGR_G_Buffer(geometry,bufferDistance,30);
+   tmp1=getMapFromMaps(outputs,"Result","mimeType");
+   if(strncmp(tmp1->value,"application/json",16)==0){
+		addToMap(outputs->content,"value",OGR_G_ExportToJson(res));
+		addToMap(outputs->content,"mimeType","text/plain");
+   }
+   else{
+		addToMap(outputs->content,"value",OGR_G_ExportToGML(res));
+   }
+   outputs->next=NULL;
+   OGR_G_DestroyGeometry(geometry);
+   OGR_G_DestroyGeometry(res);
+   return SERVICE_SUCCEEDED;
+}
+
+/*  int Buffer(maps*& conf,maps*& inputs,maps*& outputs){
+#ifdef DEBUG
+    fprintf(stderr,"\nService internal print\n");
+#endif
+    maps* cursor=inputs;
+    OGRGeometryH geometry,res;
+    int bufferDistance;    
+    if(cursor!=NULL){
+#ifdef DEBUG
+      fprintf(stderr,"\nService internal print\n");
+      dumpMaps(cursor);
+      fprintf(stderr,"\nService internal print\n");
+      dumpMaps(inputs);
+      fprintf(stderr,"\nService internal print\n");
+#endif
+      map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
+      map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
+      if(tmp1!=NULL){
+        if(strncmp(tmp1->value,"application/json",16)==0 ||
+	   strncmp(tmp1->value,"text/js",7)==0)
+      	  geometry=OGR_G_CreateGeometryFromJson(tmp->value);
+	else
+	  geometry=createGeometryFromGML(conf,tmp->value);
+      }
+      else
+      	geometry=createGeometryFromGML(conf,tmp->value);
+    }
+    if(cursor!=NULL){
+      map* tmp=getMapFromMaps(cursor,"BufferDistance","value");
+      if(tmp==NULL){
+	bufferDistance=10;
+      }
+      else
+	bufferDistance=atoi(tmp->value);
+#ifdef DEBUG
+      fprintf(stderr,"\nService internal print (BufferDistance value: %i)\n",bufferDistance);
+#endif
+    }
+#ifdef DEBUG
+    dumpMaps(outputs);
+#endif
+    map* tmp=getMapFromMaps(outputs,"Result","mimeType");
+    res=OGR_G_Buffer(geometry,bufferDistance,30);
+#ifdef DEBUG
+    dumpMap(tmp);
+#endif
+    if(tmp!=NULL){
+#ifdef DEBUG
+      dumpMap(tmp);
+#endif
+      if(strncmp(tmp->value,"application/json",16)==0){
+	addToMap(outputs->content,"value",OGR_G_ExportToJson(res));
+	addToMap(outputs->content,"mimeType","text/plain");
+	addToMap(outputs->content,"encoding","UTF-8");
+      }
+      else if(strcmp(tmp->value,"text/xml")==0){
+	xmlInitParser();
+	xmlDocPtr doc = xmlParseMemory(tmp->value,strlen(tmp->value));
+	xmlChar *xmlbuff;
+	int buffersize;
+	char *buff=OGR_G_ExportToGML(res);
+	addToMap(outputs->content,"value",buff);
+	map* tmp1=getMapFromMaps(outputs,"Result","encoding");
+	if(tmp1!=NULL)
+	  addToMap(outputs->content,"encoding",tmp1->value);
+	else
+	  addToMap(outputs->content,"encoding","UTF-8");
+	xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
+	xmlFree(xmlbuff);
+	xmlFreeDoc(doc);
+	xmlCleanupParser();
+      }
+      else{
+	addToMap(outputs->content,"value",OGR_G_ExportToJson(res));
+	addToMap(outputs->content,"mimeType","text/plain");
+	addToMap(outputs->content,"encoding","UTF-8");
+	outputs->next=NULL;
+      }
+      outputs->next=NULL;
+    }
+    else{
+      addToMap(outputs->content,"value",OGR_G_ExportToJson(res));
+      addToMap(outputs->content,"mimeType","text/plain");
+      addToMap(outputs->content,"encoding","UTF-8");
+      outputs->next=NULL;
+    }
+    outputs->next=NULL;
+#ifdef DEBUG
+    dumpMaps(outputs);
+    fprintf(stderr,"\nService internal print\n===\n");
+#endif
+    OGR_G_DestroyGeometry(geometry);
+    OGR_G_DestroyGeometry(res);
+    return SERVICE_SUCCEEDED;
+  }
+*/
+
+#ifdef WIN32
+  __declspec(dllexport)
+#endif
+  int Boundary(maps*& conf,maps*& inputs,maps*& outputs){
+    return applyOne(conf,inputs,outputs,&OGR_G_GetBoundary);
+  }
+
+#ifdef WIN32
+  __declspec(dllexport)
+#endif
+  int ConvexHull(maps*& conf,maps*& inputs,maps*& outputs){
+    return applyOne(conf,inputs,outputs,&OGR_G_ConvexHull);
+  }
+
+
+  OGRGeometryH MY_OGR_G_Centroid(OGRGeometryH hTarget){
+    OGRGeometryH res;
+    res=OGR_G_CreateGeometryFromJson("{\"type\": \"Point\", \"coordinates\": [0,0] }");
+    OGRwkbGeometryType gtype=OGR_G_GetGeometryType(hTarget);
+    if(gtype!=wkbPolygon){
+      hTarget=OGR_G_ConvexHull(hTarget);
+    }
+    int c=OGR_G_Centroid(hTarget,res);
+    return res;
+  }
+
+#ifdef WIN32
+  __declspec(dllexport)
+#endif
+  int Centroid(maps*& conf,maps*& inputs,maps*& outputs){
+    return applyOne(conf,inputs,outputs,&MY_OGR_G_Centroid);
   }
 
@@ -130,15 +423,14 @@
     }
     res=(*myFunc)(geometry1,geometry2);
-    outputs=(maps*)malloc(sizeof(maps*));
-    outputs->name="Result";
-    char tmpres[100];
-    sprintf(tmpres,"%d",res);
-    outputs->content=createMap("value",OGR_G_ExportToJson(res));
-    addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-    addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
+    addToMap(outputs->content,"value",OGR_G_ExportToJson(res));
+    addToMap(outputs->content,"mimeType","text/plain");
+    addToMap(outputs->content,"encoding","UTF-8");
     outputs->next=NULL;
+    OGR_G_DestroyGeometry(geometry1);
+    OGR_G_DestroyGeometry(geometry2);
+    OGR_G_DestroyGeometry(res);
     return SERVICE_SUCCEEDED;
   }
-
+  
 #ifdef WIN32
   __declspec(dllexport)
@@ -162,366 +454,9 @@
   }
 
-  int applyOne(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometryH (*myFunc)(OGRGeometryH)){
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print\n");
-#endif
-    maps* cursor=inputs;
-    OGRGeometryH geometry,res;
-    int bufferDistance;
-#ifdef DEBUG
-    dumpMaps(cursor);
-#endif
-    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
-    if(!tmp)
-      return SERVICE_FAILED;
-    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeTime");
-    if(tmp1!=NULL){
-      if(strncmp(tmp1->value,"text/js",7)==0)
-        geometry=OGR_G_CreateGeometryFromJson(tmp->value);
-      else
-        geometry=createGeometryFromGML(conf,tmp->value);
-      }
-    else
-      geometry=createGeometryFromGML(conf,tmp->value);
-    res=(*myFunc)(geometry);
-    /**
-     * Here we should check what is the requested output.
-     * Let's use JSON per default.
-     */
-    //outputs=(maps*)malloc(sizeof(maps*));
-    outputs->name="Result";
-    if(tmp1!=NULL){
-      if(strncmp(tmp1->value,"text/js",7)==0){
-        outputs->content=createMap("value",OGR_G_ExportToJson(tmp->value));
-	addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      }
-      else{
-        outputs->content=createMap("value",OGR_G_ExportToGML(res));
-	addMapToMap(&outputs->content,createMap("mimeType","text/xml"));
-	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-	addMapToMap(&outputs->content,createMap("schema","http://fooa/gml/3.1.0/polygon.xsd"));
-      }
-    }else{
-      outputs->content=createMap("value",OGR_G_ExportToJson(res));
-      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-    }
-    outputs->next=NULL;
-#ifdef DEBUG
-    dumpMaps(outputs);
-    fprintf(stderr,"\nService internal print\n===\n");
-#endif
-    return SERVICE_SUCCEEDED;
-  }
-
-#ifdef WIN32
-  __declspec(dllexport)
-#endif
-  int ConvexHull(maps*& conf,maps*& inputs,maps*& outputs){
-    return applyOne(conf,inputs,outputs,&OGR_G_ConvexHull);
-  }
-
-#ifdef WIN32
-  __declspec(dllexport)
-#endif
-  int Boundary(maps*& conf,maps*& inputs,maps*& outputs){
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print\nStarting\n");
-#endif
-    maps* cursor=inputs;
-    OGRGeometryH geometry,res;
-    int bufferDistance;
-    xmlInitParser();
-    
-    if(cursor!=NULL){
-      map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
-      map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
-      if(tmp1!=NULL){
-        if(strncmp(tmp1->value,"text/js",7)==0)
-      	  geometry=OGR_G_CreateGeometryFromJson(tmp->value);
-	else
-	  geometry=createGeometryFromGML(conf,tmp->value);
-      }
-      else
-      	geometry=createGeometryFromGML(conf,tmp->value);
-    }
-    res=OGR_G_GetBoundary(geometry);
-    outputs=(maps*)malloc(sizeof(maps*));
-    outputs->name="Boundary";
-    outputs->content=createMap("value",OGR_G_ExportToJson(res));
-    addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-    addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-    /*
-    outputs->content=createMap("value",OGR_G_ExportToGML(res));
-    addMapToMap(&outputs->content,createMap("mimeType","text/xml"));
-    addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-    addMapToMap(&outputs->content,createMap("schema","http://fooa/gml/3.1.0/polygon.xsd"));
-    */
-    outputs->next=NULL;
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print\n===\n");
-#endif
-    xmlCleanupParser();
-    return SERVICE_SUCCEEDED;
-    }
-
-#ifdef WIN32
-  __declspec(dllexport)
-#endif
-  int Centroid(maps*& conf,maps*& inputs,maps*& outputs){
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print\n");
-#endif
-    maps* cursor=inputs;
-    OGRGeometryH geometry,res=NULL;
-    int bufferDistance;    
-    if(cursor!=NULL){
-#ifdef DEBUG
-      fprintf(stderr,"\nService internal print\n");
-      dumpMaps(cursor);
-      fprintf(stderr,"\nService internal print\n");
-      dumpMaps(inputs);
-      fprintf(stderr,"\nService internal print\n");
-#endif
-      map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
-      map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
-      if(tmp1!=NULL){
-        if(strncmp(tmp1->value,"application/json",16)==0)
-      	  geometry=OGR_G_CreateGeometryFromJson(tmp->value);
-	else
-	  geometry=createGeometryFromGML(conf,tmp->value);
-      }
-      else
-      	geometry=createGeometryFromGML(conf,tmp->value);
-    }else{
-    /**
-     * Print WPSExceptionError message here !!
-     */
-    }
-#ifdef DEBUG
-    dumpMaps(outputs);
-#endif
-    res=OGR_G_CreateGeometryFromJson("{\"type\": \"Point\", \"coordinates\": [0,0] }");
-    OGRwkbGeometryType gtype=OGR_G_GetGeometryType(geometry);
-    if(gtype!=wkbPolygon){
-    	fprintf(stderr,"\n\nGeometry Type is not Polygon, let us use the ConvexHull !\n\n");
-    	geometry=OGR_G_ConvexHull(OGR_G_Clone(geometry));
-    }else{
-    	fprintf(stderr,"\n\nGeometry Type is Polygon, thanks !\n\n");
-    }
-    int c=OGR_G_Centroid(geometry,res);
-#ifdef DEBUG
-    fprintf(stderr,"\n\nC VALUE : %d\n\n",c);
-#endif
-    map* tmp=getMapFromMaps(outputs,"Result","mimeType");
-#ifdef DEBUG
-    dumpMaps(outputs);
-#endif
-    if(tmp!=NULL){
-#ifdef DEBUG
-      dumpMap(tmp);
-#endif
-      if(strncmp(mtoupper(tmp->value),mtoupper("application/json"),16)==0){
-	outputs->content=createMap("value",OGR_G_ExportToJson(res));
-	addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      }
-      else if(strncmp(mtoupper(tmp->value),mtoupper("text/xml"),8)==0){
-	/*xmlInitParser();
-	xmlDocPtr doc = xmlParseMemory(tmp->value,strlen(tmp->value));
-	xmlChar *xmlbuff;
-	int buffersize;*/
-	outputs->content=createMap("value",OGR_G_ExportToGML(res));
-	addMapToMap(&outputs->content,createMap("mimeType","text/xml"));
-	map* tmp1=getMapFromMaps(outputs,"Result","encoding");
-	if(tmp1!=NULL)
-	  addMapToMap(&outputs->content,createMap("encoding",tmp1->value));
-	else
-	  addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-	/*xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
-	outputs->content=createMap("value",OGR_G_ExportToGML(res));
-	xmlFree(xmlbuff);
-	xmlFreeDoc(doc);
-	xmlCleanupParser();
-	*/
-      }
-    else{
-      outputs=(maps*)malloc(sizeof(maps*));
-      outputs->name="Result";
-      outputs->content=createMap("value",OGR_G_ExportToJson(res));
-      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      outputs->next=NULL;
-    }
-    outputs->next=NULL;
-    }
-    else{
-      outputs=(maps*)malloc(sizeof(maps*));
-      outputs->name="Result";
-      outputs->content=createMap("value",OGR_G_ExportToJson(res));
-      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      outputs->next=NULL;
-    }
-    outputs->next=NULL;
-#ifdef DEBUG
-    dumpMaps(outputs);
-    fprintf(stderr,"\nService internal print\n===\n");
-#endif
-    return SERVICE_SUCCEEDED;
-  }
-
-#ifdef WIN32
-  __declspec(dllexport)
-#endif
-  int Buffer(maps*& conf,maps*& inputs,maps*& outputs){
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print\n");
-#endif
-    maps* cursor=inputs;
-    OGRGeometryH geometry,res;
-    int bufferDistance;    
-    if(cursor!=NULL){
-#ifdef DEBUG
-      fprintf(stderr,"\nService internal print\n");
-      dumpMaps(cursor);
-      fprintf(stderr,"\nService internal print\n");
-      dumpMaps(inputs);
-      fprintf(stderr,"\nService internal print\n");
-#endif
-      map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
-      map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
-      if(tmp1!=NULL){
-        if(strncmp(tmp1->value,"application/json",16)==0)
-      	  geometry=OGR_G_CreateGeometryFromJson(tmp->value);
-	else
-	  geometry=createGeometryFromGML(conf,tmp->value);
-      }
-      else
-      	geometry=createGeometryFromGML(conf,tmp->value);
-    }
-    if(cursor!=NULL){
-      map* tmp=getMapFromMaps(cursor,"BufferDistance","value");
-      if(tmp==NULL){
-	bufferDistance=10;
-      }
-      else
-	bufferDistance=atoi(tmp->value);
-#ifdef DEBUG
-      fprintf(stderr,"\nService internal print (BufferDistance value: %i)\n",bufferDistance);
-#endif
-    }
-#ifdef DEBUG
-    dumpMaps(outputs);
-#endif
-    map* tmp=getMapFromMaps(outputs,"BufferedPolygon","mimeType");
-    res=OGR_G_Buffer(geometry,bufferDistance,30);
-#ifdef DEBUG
-    dumpMap(tmp);
-#endif
-    if(tmp!=NULL){
-#ifdef DEBUG
-      dumpMap(tmp);
-#endif
-      if(strncmp(mtoupper(tmp->value),mtoupper("application/json"),16)==0){
-	outputs->content=createMap("value",OGR_G_ExportToJson(res));
-	addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-	addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      }
-      else if(strcmp(mtoupper(tmp->value),mtoupper("text/xml"))==0){
-	xmlInitParser();
-	xmlDocPtr doc = xmlParseMemory(tmp->value,strlen(tmp->value));
-	xmlChar *xmlbuff;
-	int buffersize;
-	outputs->content=createMap("value",OGR_G_ExportToGML(res));
-	addMapToMap(&outputs->content,createMap("mimeType","text/xml"));
-	map* tmp1=getMapFromMaps(outputs,"BufferedPolygon","encoding");
-	if(tmp1!=NULL)
-	  addMapToMap(&outputs->content,createMap("encoding",tmp1->value));
-	else
-	  addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-	xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
-	outputs->content=createMap("value",OGR_G_ExportToGML(res));
-	xmlFree(xmlbuff);
-	xmlFreeDoc(doc);
-	xmlCleanupParser();
-      }
-    else{
-      outputs=(maps*)malloc(sizeof(maps*));
-      outputs->name="BufferedPolygon";
-      outputs->content=createMap("value",OGR_G_ExportToJson(res));
-      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      outputs->next=NULL;
-    }
-    outputs->next=NULL;
-    }
-    else{
-      outputs=(maps*)malloc(sizeof(maps*));
-      outputs->name="BufferedPolygon";
-      outputs->content=createMap("value",OGR_G_ExportToJson(res));
-      addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-      addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-      outputs->next=NULL;
-    }
-    outputs->next=NULL;
-#ifdef DEBUG
-    dumpMaps(outputs);
-    fprintf(stderr,"\nService internal print\n===\n");
-#endif
-    return SERVICE_SUCCEEDED;
-  }
-
 #ifdef WIN32
   __declspec(dllexport)
 #endif
   int Union(maps*& conf,maps*& inputs,maps*& outputs){
-#ifdef DEBUG
-    fprintf(stderr,"\nService internal print1\n");
-    fflush(stderr);
-#endif
-    maps* cursor=inputs;
-    OGRGeometryH geometry1,geometry2;
-    OGRGeometryH res;
-    {
-      map* tmp=getMapFromMaps(inputs,"InputEntity1","value");
-      map* tmp1=getMapFromMaps(inputs,"InputEntity1","mimeType");
-      if(tmp1!=NULL){
-        if(strncmp(tmp1->value,"application/json",16)==0)
-      	  geometry1=OGR_G_CreateGeometryFromJson(tmp->value);
-	else
-	  geometry1=createGeometryFromGML(conf,tmp->value);
-      }
-      else
-      	geometry1=createGeometryFromGML(conf,tmp->value);
-    }
-    {
-      map* tmp=getMapFromMaps(inputs,"InputEntity2","value");
-      map* tmp1=getMapFromMaps(inputs,"InputEntity2","mimeType");
-#ifdef DEBUG
-      fprintf(stderr,"MY MAP\n");
-      dumpMap(tmp1);
-      fprintf(stderr,"MY MAP\n");
-#endif
-      if(tmp1!=NULL){
-        if(strncmp(tmp1->value,"application/json",16)==0)
-      	  geometry2=OGR_G_CreateGeometryFromJson(tmp->value);
-	else
-	  geometry2=createGeometryFromGML(conf,tmp->value);
-      }
-      else
-      	geometry2=createGeometryFromGML(conf,tmp->value);
-    }
-    res=OGR_G_Union(geometry1,geometry2);
-    outputs=(maps*)malloc(sizeof(maps*));
-    outputs->name="Result";
-    char tmpres[100];
-    sprintf(tmpres,"%d",res);
-    outputs->content=createMap("value",OGR_G_ExportToJson(res));
-    addMapToMap(&outputs->content,createMap("mimeType","text/plain"));
-    addMapToMap(&outputs->content,createMap("encoding","UTF-8"));
-    outputs->next=NULL;
-    return SERVICE_SUCCEEDED;
+    return applyTwo(conf,inputs,outputs,&OGR_G_Union);
   }
 
@@ -589,4 +524,7 @@
   }
 
+#ifdef WIN32
+  __declspec(dllexport)
+#endif
   int GetArea(maps*& conf,maps*& inputs,maps*& outputs){
     fprintf(stderr,"GETAREA \n");
