Ignore:
Timestamp:
Aug 13, 2014, 3:20:40 PM (10 years ago)
Author:
djay
Message:

Fix Simplify service to first build against GDAL 1.11.0 and to be able to handle FeatureCollection? as other services.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-services/ogr/base-vect-ops/service.c

    r491 r497  
    104104#endif
    105105  int Simplify(maps*& conf,maps*& inputs,maps*& outputs){
    106     maps* cursor=inputs;
    107     OGRGeometryH geometry,res;
     106    OGRRegisterAll();
     107
    108108    double tolerance;
    109     map* tmp0=getMapFromMaps(cursor,"Tolerance","value");
     109    map* tmp0=getMapFromMaps(inputs,"Tolerance","value");
    110110    if(tmp0==NULL){
    111111      tolerance=atof("2.0");
     
    113113    else
    114114      tolerance=atof(tmp0->value);
    115 #ifdef DEBUG
    116     fprintf(stderr,"Tolerance for Simplify %f",tolerance);
    117 #endif
     115
     116    maps* cursor=inputs;
     117    OGRGeometryH geometry;
     118    OGRGeometry *res;
     119    OGRLayer *poDstLayer;
     120    const char *oDriver1;
     121    OGRDataSource       *poODS;
    118122    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
    119123    if(!tmp){
     
    121125      return SERVICE_FAILED;
    122126    }
     127    char filename[1024];
    123128    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
     129    const char *oDriver;
     130    oDriver="GeoJSON";
     131    sprintf(filename,"/vsimem/input_%d.json",getpid());
    124132    if(tmp1!=NULL){
    125       if(strncmp(tmp1->value,"text/js",7)==0 ||
    126          strncmp(tmp1->value,"application/json",16)==0)
    127         geometry=OGR_G_CreateGeometryFromJson(tmp->value);
    128       else
    129         geometry=createGeometryFromGML(conf,tmp->value);
    130     }
    131     else{
    132       setMapInMaps(conf,"lenv","message",_ss("Unable to find any geometry for InputPolygon"));
     133      if(strcmp(tmp1->value,"text/xml")==0){
     134        sprintf(filename,"/vsimem/input_%d.xml",getpid());
     135        oDriver="GML";
     136      }
     137    }
     138    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
     139    VSIFCloseL(ifile);
     140    OGRDataSource* ipoDS = OGRSFDriverRegistrar::Open(filename,FALSE);
     141    char pszDestDataSource[100];
     142    if( ipoDS == NULL )
     143      {
     144        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
     145       
     146        fprintf( stderr, "FAILURE:\n"
     147                 "Unable to open datasource `%s' with the following drivers.\n",
     148                 filename );
     149       
     150        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
     151          {
     152            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
     153          }
     154        char tmp[1024];
     155        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
     156        setMapInMaps(conf,"lenv","message",tmp);
     157        return SERVICE_FAILED;
     158      }
     159    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
     160         iLayer++ )
     161      {
     162        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
     163       
     164        if( poLayer == NULL )
     165          {
     166            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
     167                     iLayer );
     168            char tmp[1024];
     169            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
     170            setMapInMaps(conf,"lenv","message",tmp);
     171            return SERVICE_FAILED;
     172          }
     173       
     174        OGRFeature  *poFeature;
     175       
     176        OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
     177        OGRSFDriver          *poDriver = NULL;
     178        int                  iDriver;
     179       
     180        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
     181        oDriver1="GeoJSON";
     182        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
     183        if(tmpMap!=NULL){
     184          if(strcmp(tmpMap->value,"text/xml")==0){
     185            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
     186            oDriver1="GML";
     187          }
     188        }
     189       
     190        for( iDriver = 0;
     191             iDriver < poR->GetDriverCount() && poDriver == NULL;
     192             iDriver++ )
     193          {
     194            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
     195              {
     196                poDriver = poR->GetDriver(iDriver);
     197              }
     198          }
     199       
     200        if( poDriver == NULL )
     201          {
     202            char emessage[8192];
     203            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
     204            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
     205           
     206            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
     207              {
     208                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
     209              }
     210           
     211            setMapInMaps(conf,"lenv","message",emessage);
     212            return SERVICE_FAILED;
     213           
     214          }
     215       
     216        if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
     217          char emessage[1024];
     218          sprintf( emessage,  "%s driver does not support data source creation.\n",
     219                   "json" );
     220          setMapInMaps(conf,"lenv","message",emessage);
     221          return SERVICE_FAILED;
     222        }
     223       
     224        char **papszDSCO=NULL;
     225        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
     226        if( poODS == NULL ){
     227          char emessage[1024];     
     228          sprintf( emessage,  "%s driver failed to create %s\n",
     229                   "json", pszDestDataSource );
     230          setMapInMaps(conf,"lenv","message",emessage);
     231          return SERVICE_FAILED;
     232        }
     233       
     234        if( !poODS->TestCapability( ODsCCreateLayer ) )
     235          {
     236            char emessage[1024];
     237            sprintf( emessage,
     238                     "Layer %s not found, and CreateLayer not supported by driver.",
     239                     "Result" );
     240            setMapInMaps(conf,"lenv","message",emessage);
     241            return SERVICE_FAILED;
     242          }
     243       
     244        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
     245        if( poDstLayer == NULL ){
     246          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
     247          return SERVICE_FAILED;
     248        }
     249       
     250        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
     251        int iField;
     252        int hasMmField=0;
     253       
     254        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
     255          {
     256            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
     257            if (iField >= 0)
     258                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
     259            else
     260            {
     261                fprintf( stderr, "Field '%s' not found in source layer.\n",
     262                        iField );
     263                return SERVICE_FAILED;
     264            }
     265          }
     266
     267        while(TRUE){
     268          OGRFeature      *poDstFeature = NULL;
     269          poFeature = poLayer->GetNextFeature();
     270          if( poFeature == NULL )
     271            break;
     272          if(poFeature->GetGeometryRef() != NULL){
     273            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
     274            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
     275              {
     276                char tmpMsg[1024];
     277                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
     278                         poFeature->GetFID(), poFDefn->GetName() );
     279               
     280                OGRFeature::DestroyFeature( poFeature );
     281                OGRFeature::DestroyFeature( poDstFeature );
     282                return SERVICE_FAILED;
     283              }
     284            geometry=poFeature->GetGeometryRef();
     285#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
     286            GEOSGeometry* ggeometry=((OGRGeometry *) geometry)->exportToGEOS();
     287            GEOSGeometry* gres=GEOSTopologyPreserveSimplify(ggeometry,tolerance);
     288            if(gres!=NULL)
     289              res=(OGRGeometry*)OGRGeometryFactory::createFromGEOS(gres);
     290#else
     291            res=((OGRGeometry *) geometry)->SimplifyPreserveTopology(tolerance);
     292#endif
     293            if(poDstFeature->SetGeometryDirectly(res) != OGRERR_NONE )
     294              {
     295                char tmpMsg[1024];
     296                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
     297                         poFeature->GetFID(), poFDefn->GetName() );
     298               
     299                OGRFeature::DestroyFeature( poFeature );
     300                OGRFeature::DestroyFeature( poDstFeature );
     301                return SERVICE_FAILED;
     302              }
     303            OGRFeature::DestroyFeature( poFeature );
     304            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
     305              {         
     306                OGRFeature::DestroyFeature( poDstFeature );
     307                return SERVICE_FAILED;
     308              }
     309            OGRFeature::DestroyFeature( poDstFeature );
     310#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
     311            GEOSGeom_destroy( ggeometry);
     312            GEOSGeom_destroy( gres);
     313#endif
     314          }
     315        }
     316      }
     317
     318    delete poODS;
     319    delete ipoDS;
     320
     321    char *res1=readVSIFile(conf,pszDestDataSource);
     322    if(res1==NULL)
    133323      return SERVICE_FAILED;
    134     }
    135     if(geometry==NULL){
    136       setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
    137       return SERVICE_FAILED;
    138     }
    139 #ifdef DEBUG
    140     fprintf(stderr,"Create GEOSGeometry object");
    141 #endif
    142     GEOSGeometry* ggeometry=((OGRGeometry *) geometry)->exportToGEOS();
    143     GEOSGeometry* gres=GEOSTopologyPreserveSimplify(ggeometry,tolerance);
    144     res=(OGRGeometryH)OGRGeometryFactory::createFromGEOS(gres);
    145     tmp1=getMapFromMaps(outputs,"Result","mimeType");
    146     if(tmp1!=NULL){
    147       if(strncmp(tmp1->value,"text/js",7)==0 ||
    148          strncmp(tmp1->value,"application/json",16)==0){
    149         char *tmpS=OGR_G_ExportToJson(res);
    150         setMapInMaps(outputs,"Result","value",tmpS);
    151 #ifndef WIN32
    152         setMapInMaps(outputs,"Result","mimeType","text/plain");
    153         setMapInMaps(outputs,"Result","encoding","UTF-8");
    154         free(tmpS);
    155 #endif
    156       }
    157       else{
    158         char *tmpS=OGR_G_ExportToGML(res);
    159         setMapInMaps(outputs,"Result","value",tmpS);
    160 #ifndef WIN32
    161         setMapInMaps(outputs,"Result","mimeType","text/xml");
    162         setMapInMaps(outputs,"Result","encoding","UTF-8");
    163         setMapInMaps(outputs,"Result","schema","http://fooa/gml/3.1.0/polygon.xsd");
    164         free(tmpS);
    165 #endif
    166       }
    167     }else{
    168       char *tmpS=OGR_G_ExportToJson(res);
    169       setMapInMaps(outputs,"Result","value",tmpS);
    170 #ifndef WIN32
    171       setMapInMaps(outputs,"Result","mimeType","text/plain");
    172       setMapInMaps(outputs,"Result","encoding","UTF-8");
    173       free(tmpS);
    174 #endif
    175     }
    176     outputs->next=NULL;
    177     //GEOSFree(ggeometry);
    178     //GEOSFree(gres);
    179     OGR_G_DestroyGeometry(res);
    180     OGR_G_DestroyGeometry(geometry);
     324    setMapInMaps(outputs,"Result","value",res1);
     325    free(res1);
     326
     327    OGRCleanupAll();
    181328    return SERVICE_SUCCEEDED;
    182   }
    183 
     329
     330}
    184331
    185332  int applyOne(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometry* (OGRGeometry::*myFunc)() const,const char* schema){
     
    355502            break;
    356503          if(poFeature->GetGeometryRef() != NULL){
    357             // DO SOMETHING HERE !!
    358504            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
    359505            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
     
    553699            return SERVICE_FAILED;
    554700          }
    555        
    556         //CPLErrorReset();
    557        
     701
    558702        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
    559703        if( poDstLayer == NULL ){
     
    585729            break;
    586730          if(poFeature->GetGeometryRef() != NULL){
    587             // DO SOMETHING HERE !!
    588731            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
    589732            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
     
    739882            break;
    740883          if(poFeature->GetGeometryRef() != NULL){
    741             // DO SOMETHING HERE !!
    742884            if((poFeature->GetGeometryRef()->*myFunc)()==0){
    743885              setMapInMaps(outputs,"Result","value","false");
     
    9751117
    9761118                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
    977                   // DO SOMETHING HERE !!
    9781119                  poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
    9791120                  if( poDstFeature->SetFrom( poFeature2, TRUE ) != OGRERR_NONE )
     
    11371278                  break;
    11381279                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
    1139                   // DO SOMETHING HERE !!
    11401280                  if((poFeature1->GetGeometryRef()->*myFunc)(poFeature2->GetGeometryRef())==0){
    11411281                    setMapInMaps(outputs,"Result","value","false");
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png