int DWithin(maps*& conf,maps*& inputs,maps*& outputs){ #ifdef DEBUG fprintf(stderr, "Starting DWithin Process...\n"); #endif OGRRegisterAll(); OGRLayerH hLayer1, hLayer2, hDstLayer, hBufferLayer = NULL; OGRFeatureDefnH hDefnLayer1; OGRDataSourceH hDS1, hDS2, hODS, hBufferDS = NULL; OGRSFDriverH hDriver; const char *psDriver = NULL; char *res1; char pszDestDataSource[100]; char pszBufferDataSource[100]; char *filename1 = (char*)malloc(1024*sizeof(char)); char *filename2 = (char*)malloc(1024*sizeof(char)); float dBufferDistance = 0.0f; map *tmpMap = NULL; int gdal_err = OGRERR_NONE; int ret = SERVICE_SUCCEEDED; map* pmBufferDistance = getMapFromMaps(inputs, "BufferDistance", "value"); if (pmBufferDistance == NULL) { setError(conf, "Could not get the BufferDistance parameter.\n"); ret = SERVICE_FAILED; goto done; } dBufferDistance = atof(pmBufferDistance->value); /* * Switch InputEntity 1 and 2 to be more logical */ hDS1 = loadEntity(conf, inputs, &filename1, "InputEntity2", 2); hDS2 = loadEntity(conf, inputs, &filename2, "InputEntity1", 1); if( hDS1 == NULL || hDS2 == NULL ) { if( hDS1 == NULL ) setError(conf, "Unabled to open dataset 2 '%s'.\n", filename1); if( hDS2 == NULL ) setError(conf, "Unabled to open dataset 1 '%s'.\n", filename2); ret = SERVICE_FAILED; goto done; } hLayer1 = OGR_DS_GetLayer(hDS1, 0); if(hLayer1 == NULL) { setError(conf, "Could not fetch first input layer.\n"); ret = SERVICE_FAILED; goto done; } hDefnLayer1 = OGR_L_GetLayerDefn(hLayer1); hLayer2 = OGR_DS_GetLayer(hDS2, 0); if(hLayer2 == NULL) { setError(conf, "Could not fetch second input layer.\n"); ret = SERVICE_FAILED; goto done; } tmpMap = getMapFromMaps(outputs, "Result", "mimeType"); if(tmpMap != NULL) { if(strcmp(tmpMap->value, "text/xml") == 0) { sprintf(pszDestDataSource, "/vsimem/result_%d.xml", getpid()); sprintf(pszBufferDataSource, "/vsimem/rbuffer_%d.xml", getpid()); psDriver = "GML"; } else if (strcmp(tmpMap->value, "application/json") == 0) { sprintf(pszDestDataSource, "/vsimem/result_%d.json", getpid()); sprintf(pszBufferDataSource, "/vsimem/buffer_%d.json", getpid()); psDriver = "GeoJSON"; } } if (psDriver == NULL) { setError(conf, "Driver '%s' cannot be used as output.\n", tmpMap->value); ret = SERVICE_FAILED; goto done; } hDriver = OGRGetDriverByName(psDriver); if(hDriver == NULL) { setError(conf, "Error loading Driver '%s'.\n", psDriver); ret = SERVICE_FAILED; goto done; } hODS = OGR_Dr_CreateDataSource(hDriver, pszDestDataSource, NULL); if (hODS == NULL) { setError(conf, "Unable to open datasource '%s' with the following driver '%s'", pszDestDataSource, psDriver); ret = SERVICE_FAILED; goto done; } hDstLayer = OGR_DS_CreateLayer(hODS, "Result", NULL, wkbUnknown, NULL); if (hDstLayer == NULL) { setError(conf, "Could not create layer 'Result'.\n"); ret = SERVICE_FAILED; goto done; } hBufferDS = OGR_Dr_CreateDataSource(hDriver, pszBufferDataSource, NULL); hBufferLayer = OGR_DS_CreateLayer(hBufferDS, "Buffer", NULL, wkbUnknown, NULL); if (set_layerDefn(hBufferLayer, hDefnLayer1) != OGRERR_NONE) { setError(conf, "Could not copy layer definition"); ret = SERVICE_FAILED; goto done; } // We buffer every geoms from the first layer OGR_L_ResetReading(hLayer1); while(OGRFeatureH hFeat1 = OGR_L_GetNextFeature(hLayer1)) { OGRGeometryH hGeom = OGR_F_GetGeometryRef(hFeat1); if(hGeom == NULL) { OGR_L_DeleteFeature(hLayer1, OGR_F_GetFID(hFeat1)); OGR_F_Destroy(hFeat1); continue; } OGRGeometryH hGeomBuf = OGR_G_Buffer(hGeom, dBufferDistance, 30); if(hGeomBuf == NULL) { setError(conf, "Error while buffering Geometry.\n"); OGR_F_Destroy(hFeat1); } OGR_F_SetGeometryDirectly(hFeat1, hGeomBuf); OGR_L_CreateFeature(hBufferLayer, hFeat1); OGR_F_Destroy(hFeat1); } OGR_DS_Destroy(hBufferDS); #ifdef DEBUG fprintf(stderr, "Buffering hLayer1 done!\n"); #endif hBufferDS = OGROpen(pszBufferDataSource, FALSE, NULL); hBufferLayer = OGR_DS_GetLayer(hBufferDS, 0); OGR_L_ResetReading(hBufferLayer); if (OGR_L_GetFeatureCount(hBufferLayer, TRUE) > 0) { if(OGR_Priv_Intersect(hLayer2, hBufferLayer, hDstLayer,"intersect") != OGRERR_NONE) { setError(conf, "Could not intersect buffered layer and input 2"); ret = SERVICE_FAILED; goto done; } } OGR_DS_Destroy(hODS); hODS = NULL; res1=readVSIFile(conf,pszDestDataSource); if(res1==NULL) return SERVICE_FAILED; setMapInMaps(outputs,"Result","value",res1); free(res1); #ifdef DEBUG fprintf(stderr, "DWithin process Done!\n"); #endif done: if (hODS != NULL) OGR_DS_Destroy(hODS); if (hDS1 != NULL) OGR_DS_Destroy(hDS1); if (hDS2 != NULL) OGR_DS_Destroy(hDS2); if (hBufferDS != NULL) OGR_DS_Destroy(hBufferDS); free(filename1); free(filename2); return ret; }