int NIImporter_ArcView::getLaneNo(OGRFeature& poFeature, const std::string& edgeid, SUMOReal speed) { if (myOptions.isSet("shapefile.type-id")) { return (int) myTypeCont.getNumLanes(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str()))); } // try to get definitions as to be found in SUMO-XML-definitions // idea by John Michael Calandrino int index = poFeature.GetDefnRef()->GetFieldIndex("nolanes"); if (index >= 0 && poFeature.IsFieldSet(index)) { return (int) poFeature.GetFieldAsInteger(index); } index = poFeature.GetDefnRef()->GetFieldIndex("NOLANES"); if (index >= 0 && poFeature.IsFieldSet(index)) { return (int) poFeature.GetFieldAsInteger(index); } index = poFeature.GetDefnRef()->GetFieldIndex("rnol"); if (index >= 0 && poFeature.IsFieldSet(index)) { return (int) poFeature.GetFieldAsInteger(index); } index = poFeature.GetDefnRef()->GetFieldIndex("LANE_CAT"); if (index >= 0 && poFeature.IsFieldSet(index)) { std::string def = poFeature.GetFieldAsString(index); return NINavTeqHelper::getLaneNumber(edgeid, def, speed); } return 0; }
static swq_expr_node* OGRFeatureFetcher(swq_expr_node *op, void *pFeatureIn) { OGRFeature *poFeature = (OGRFeature*) pFeatureIn; swq_expr_node *poRetNode = NULL; switch (op->field_type) { case SWQ_INTEGER: case SWQ_BOOLEAN: poRetNode = new swq_expr_node( poFeature->GetFieldAsInteger(op->field_index)); break; case SWQ_FLOAT: poRetNode = new swq_expr_node( poFeature->GetFieldAsDouble(op->field_index)); break; default: poRetNode = new swq_expr_node( poFeature->GetFieldAsString(op->field_index)); break; } poRetNode->is_null = !(poFeature->IsFieldSet(op->field_index)); return poRetNode; }
static void OGR2SQLITE_ogr_geocode_set_result(sqlite3_context* pContext, OGRLayerH hLayer, const char* pszField) { if( hLayer == NULL ) sqlite3_result_null (pContext); else { OGRLayer* poLayer = (OGRLayer*)hLayer; OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn(); OGRFeature* poFeature = poLayer->GetNextFeature(); int nIdx = -1; if( poFeature == NULL ) sqlite3_result_null (pContext); else if( strcmp(pszField, "geometry") == 0 && poFeature->GetGeometryRef() != NULL ) { GByte* pabyGeomBLOB = NULL; int nGeomBLOBLen = 0; if( OGRSQLiteLayer::ExportSpatiaLiteGeometry( poFeature->GetGeometryRef(), 4326, wkbNDR, FALSE, FALSE, FALSE, &pabyGeomBLOB, &nGeomBLOBLen ) != CE_None ) { sqlite3_result_null (pContext); } else { sqlite3_result_blob (pContext, pabyGeomBLOB, nGeomBLOBLen, CPLFree); } } else if( (nIdx = poFDefn->GetFieldIndex(pszField)) >= 0 && poFeature->IsFieldSet(nIdx) ) { OGRFieldType eType = poFDefn->GetFieldDefn(nIdx)->GetType(); if( eType == OFTInteger ) sqlite3_result_int(pContext, poFeature->GetFieldAsInteger(nIdx)); else if( eType == OFTInteger64 ) sqlite3_result_int64(pContext, poFeature->GetFieldAsInteger64(nIdx)); else if( eType == OFTReal ) sqlite3_result_double(pContext, poFeature->GetFieldAsDouble(nIdx)); else sqlite3_result_text(pContext, poFeature->GetFieldAsString(nIdx), -1, SQLITE_TRANSIENT); } else sqlite3_result_null (pContext); delete poFeature; OGRGeocodeFreeResult(hLayer); } }
int NIImporter_ArcView::getPriority(OGRFeature& poFeature, const std::string& /*edgeid*/) { if (myOptions.isSet("shapefile.type-id")) { return myTypeCont.getPriority(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str()))); } // try to get definitions as to be found in SUMO-XML-definitions // idea by John Michael Calandrino int index = poFeature.GetDefnRef()->GetFieldIndex("priority"); if (index >= 0 && poFeature.IsFieldSet(index)) { return poFeature.GetFieldAsInteger(index); } index = poFeature.GetDefnRef()->GetFieldIndex("PRIORITY"); if (index >= 0 && poFeature.IsFieldSet(index)) { return poFeature.GetFieldAsInteger(index); } // try to determine priority from NavTechs FUNC_CLASS attribute index = poFeature.GetDefnRef()->GetFieldIndex("FUNC_CLASS"); if (index >= 0 && poFeature.IsFieldSet(index)) { return poFeature.GetFieldAsInteger(index); } return 0; }
SUMOReal NIImporter_ArcView::getSpeed(OGRFeature& poFeature, const std::string& edgeid) { if (myOptions.isSet("shapefile.type-id")) { return myTypeCont.getSpeed(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str()))); } // try to get definitions as to be found in SUMO-XML-definitions // idea by John Michael Calandrino int index = poFeature.GetDefnRef()->GetFieldIndex("speed"); if (index >= 0 && poFeature.IsFieldSet(index)) { return (SUMOReal) poFeature.GetFieldAsDouble(index); } index = poFeature.GetDefnRef()->GetFieldIndex("SPEED"); if (index >= 0 && poFeature.IsFieldSet(index)) { return (SUMOReal) poFeature.GetFieldAsDouble(index); } // try to get the NavTech-information index = poFeature.GetDefnRef()->GetFieldIndex("SPEED_CAT"); if (index >= 0 && poFeature.IsFieldSet(index)) { std::string def = poFeature.GetFieldAsString(index); return NINavTeqHelper::getSpeed(edgeid, def); } return -1; }
static swq_expr_node *OGRFeatureFetcher( swq_expr_node *op, void *pFeatureIn ) { OGRFeature *poFeature = (OGRFeature *) pFeatureIn; swq_expr_node *poRetNode = NULL; if( op->field_type == SWQ_GEOMETRY ) { int iField = op->field_index - (poFeature->GetFieldCount() + SPECIAL_FIELD_COUNT); poRetNode = new swq_expr_node( poFeature->GetGeomFieldRef(iField) ); return poRetNode; } switch( op->field_type ) { case SWQ_INTEGER: case SWQ_BOOLEAN: poRetNode = new swq_expr_node( poFeature->GetFieldAsInteger(op->field_index) ); break; case SWQ_INTEGER64: poRetNode = new swq_expr_node( poFeature->GetFieldAsInteger64(op->field_index) ); break; case SWQ_FLOAT: poRetNode = new swq_expr_node( poFeature->GetFieldAsDouble(op->field_index) ); break; default: poRetNode = new swq_expr_node( poFeature->GetFieldAsString(op->field_index) ); break; } poRetNode->is_null = !(poFeature->IsFieldSet(op->field_index)); return poRetNode; }
OGRFeature* OGRPLScenesLayer::GetNextRawFeature() { if( bEOF || (!bFilterMustBeClientSideEvaluated && nFeatureCount >= 0 && nNextFID > nFeatureCount) ) return NULL; if( poGeoJSONLayer == NULL ) { if( !GetNextPage() ) return NULL; } #ifdef notdef if( CSLTestBoolean(CPLGetConfigOption("OGR_LIMIT_TOO_MANY_FEATURES", "FALSE")) && nFeatureCount > nPageSize ) { bEOF = TRUE; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); OGRGeometry* poGeom; const char* pszWKT = "MULTIPOLYGON(((-180 90,180 90,180 -90,-180 -90,-180 90)))"; OGRGeometryFactory::createFromWkt((char**)&pszWKT, poSRS, &poGeom); poFeature->SetGeometryDirectly(poGeom); return poFeature; } #endif OGRFeature* poGeoJSONFeature = poGeoJSONLayer->GetNextFeature(); if( poGeoJSONFeature == NULL ) { osRequestURL = osNextURL; bStillInFirstPage = FALSE; if( !GetNextPage() ) return NULL; poGeoJSONFeature = poGeoJSONLayer->GetNextFeature(); if( poGeoJSONFeature == NULL ) { bEOF = TRUE; return NULL; } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID++); OGRGeometry* poGeom = poGeoJSONFeature->StealGeometry(); if( poGeom != NULL ) { if( poGeom->getGeometryType() == wkbPolygon ) { OGRMultiPolygon* poMP = new OGRMultiPolygon(); poMP->addGeometryDirectly(poGeom); poGeom = poMP; } poGeom->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poGeom); } for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(i); OGRFieldType eType = poFieldDefn->GetType(); int iSrcField = poGeoJSONFeature->GetFieldIndex(poFieldDefn->GetNameRef()); if( iSrcField >= 0 && poGeoJSONFeature->IsFieldSet(iSrcField) ) { if( eType == OFTInteger ) poFeature->SetField(i, poGeoJSONFeature->GetFieldAsInteger(iSrcField)); else if( eType == OFTReal ) poFeature->SetField(i, poGeoJSONFeature->GetFieldAsDouble(iSrcField)); else poFeature->SetField(i, poGeoJSONFeature->GetFieldAsString(iSrcField)); } } delete poGeoJSONFeature; return poFeature; }
void NIImporter_ArcView::load() { #ifdef HAVE_GDAL PROGRESS_BEGIN_MESSAGE("Loading data from '" + mySHPName + "'"); #if GDAL_VERSION_MAJOR < 2 OGRRegisterAll(); OGRDataSource* poDS = OGRSFDriverRegistrar::Open(mySHPName.c_str(), FALSE); #else GDALAllRegister(); GDALDataset* poDS = (GDALDataset*)GDALOpenEx(mySHPName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL); #endif if (poDS == NULL) { WRITE_ERROR("Could not open shape description '" + mySHPName + "'."); return; } // begin file parsing OGRLayer* poLayer = poDS->GetLayer(0); poLayer->ResetReading(); // build coordinate transformation OGRSpatialReference* origTransf = poLayer->GetSpatialRef(); OGRSpatialReference destTransf; // use wgs84 as destination destTransf.SetWellKnownGeogCS("WGS84"); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf); if (poCT == NULL) { if (myOptions.isSet("shapefile.guess-projection")) { OGRSpatialReference origTransf2; origTransf2.SetWellKnownGeogCS("WGS84"); poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf); } if (poCT == 0) { WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed."); } } OGRFeature* poFeature; poLayer->ResetReading(); while ((poFeature = poLayer->GetNextFeature()) != NULL) { // read in edge attributes std::string id, name, from_node, to_node; if (!getStringEntry(poFeature, "shapefile.street-id", "LINK_ID", true, id)) { WRITE_ERROR("Needed field '" + id + "' (from node id) is missing."); } if (id == "") { WRITE_ERROR("Could not obtain edge id."); return; } getStringEntry(poFeature, "shapefile.street-id", "ST_NAME", true, name); name = StringUtils::replace(name, "&", "&"); if (!getStringEntry(poFeature, "shapefile.from-id", "REF_IN_ID", true, from_node)) { WRITE_ERROR("Needed field '" + from_node + "' (from node id) is missing."); } if (!getStringEntry(poFeature, "shapefile.to-id", "NREF_IN_ID", true, to_node)) { WRITE_ERROR("Needed field '" + to_node + "' (to node id) is missing."); } if (from_node == "" || to_node == "") { from_node = toString(myRunningNodeID++); to_node = toString(myRunningNodeID++); } std::string type; if (myOptions.isSet("shapefile.type-id") && poFeature->GetFieldIndex(myOptions.getString("shapefile.type-id").c_str()) >= 0) { type = poFeature->GetFieldAsString(myOptions.getString("shapefile.type-id").c_str()); } else if (poFeature->GetFieldIndex("ST_TYP_AFT") >= 0) { type = poFeature->GetFieldAsString("ST_TYP_AFT"); } SUMOReal width = myTypeCont.getWidth(type); SUMOReal speed = getSpeed(*poFeature, id); int nolanes = getLaneNo(*poFeature, id, speed); int priority = getPriority(*poFeature, id); if (nolanes == 0 || speed == 0) { if (myOptions.getBool("shapefile.use-defaults-on-failure")) { nolanes = myTypeCont.getNumLanes(""); speed = myTypeCont.getSpeed(""); } else { OGRFeature::DestroyFeature(poFeature); WRITE_ERROR("The description seems to be invalid. Please recheck usage of types."); return; } } if (mySpeedInKMH) { speed = speed / (SUMOReal) 3.6; } // read in the geometry OGRGeometry* poGeometry = poFeature->GetGeometryRef(); OGRwkbGeometryType gtype = poGeometry->getGeometryType(); assert(gtype == wkbLineString); UNUSED_PARAMETER(gtype); // ony used for assertion OGRLineString* cgeom = (OGRLineString*) poGeometry; if (poCT != 0) { // try transform to wgs84 cgeom->transform(poCT); } PositionVector shape; for (int j = 0; j < cgeom->getNumPoints(); j++) { Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j)); if (!NBNetBuilder::transformCoordinate(pos)) { WRITE_WARNING("Unable to project coordinates for edge '" + id + "'."); } shape.push_back_noDoublePos(pos); } // build from-node NBNode* from = myNodeCont.retrieve(from_node); if (from == 0) { Position from_pos = shape[0]; from = myNodeCont.retrieve(from_pos); if (from == 0) { from = new NBNode(from_node, from_pos); if (!myNodeCont.insert(from)) { WRITE_ERROR("Node '" + from_node + "' could not be added"); delete from; continue; } } } // build to-node NBNode* to = myNodeCont.retrieve(to_node); if (to == 0) { Position to_pos = shape[-1]; to = myNodeCont.retrieve(to_pos); if (to == 0) { to = new NBNode(to_node, to_pos); if (!myNodeCont.insert(to)) { WRITE_ERROR("Node '" + to_node + "' could not be added"); delete to; continue; } } } if (from == to) { WRITE_WARNING("Edge '" + id + "' connects identical nodes, skipping."); continue; } // retrieve the information whether the street is bi-directional std::string dir; int index = poFeature->GetDefnRef()->GetFieldIndex("DIR_TRAVEL"); if (index >= 0 && poFeature->IsFieldSet(index)) { dir = poFeature->GetFieldAsString(index); } // add positive direction if wanted if (dir == "B" || dir == "F" || dir == "" || myOptions.getBool("shapefile.all-bidirectional")) { if (myEdgeCont.retrieve(id) == 0) { LaneSpreadFunction spread = dir == "B" || dir == "FALSE" ? LANESPREAD_RIGHT : LANESPREAD_CENTER; NBEdge* edge = new NBEdge(id, from, to, type, speed, nolanes, priority, width, NBEdge::UNSPECIFIED_OFFSET, shape, name, id, spread); myEdgeCont.insert(edge); checkSpread(edge); } } // add negative direction if wanted if (dir == "B" || dir == "T" || myOptions.getBool("shapefile.all-bidirectional")) { if (myEdgeCont.retrieve("-" + id) == 0) { LaneSpreadFunction spread = dir == "B" || dir == "FALSE" ? LANESPREAD_RIGHT : LANESPREAD_CENTER; NBEdge* edge = new NBEdge("-" + id, to, from, type, speed, nolanes, priority, width, NBEdge::UNSPECIFIED_OFFSET, shape.reverse(), name, id, spread); myEdgeCont.insert(edge); checkSpread(edge); } } // OGRFeature::DestroyFeature(poFeature); } #if GDAL_VERSION_MAJOR < 2 OGRDataSource::DestroyDataSource(poDS); #else GDALClose(poDS); #endif PROGRESS_DONE_MESSAGE(); #else WRITE_ERROR("SUMO was compiled without GDAL support."); #endif }
OGRFeatureDefn * OGRCARTODBTableLayer::GetLayerDefnInternal(CPL_UNUSED json_object* poObjIn) { if( poFeatureDefn != NULL ) return poFeatureDefn; CPLString osCommand; if( poDS->IsAuthenticatedConnection() ) { // Get everything ! osCommand.Printf( "SELECT a.attname, t.typname, a.attlen, " "format_type(a.atttypid,a.atttypmod), " "a.attnum, " "a.attnotnull, " "i.indisprimary, " "pg_get_expr(def.adbin, c.oid) AS defaultexpr, " "postgis_typmod_dims(a.atttypmod) dim, " "postgis_typmod_srid(a.atttypmod) srid, " "postgis_typmod_type(a.atttypmod)::text geomtyp, " "srtext " "FROM pg_class c " "JOIN pg_attribute a ON a.attnum > 0 AND " "a.attrelid = c.oid AND c.relname = '%s' " "JOIN pg_type t ON a.atttypid = t.oid " "JOIN pg_namespace n ON c.relnamespace=n.oid AND n.nspname= '%s' " "LEFT JOIN pg_index i ON c.oid = i.indrelid AND " "i.indisprimary = 't' AND a.attnum = ANY(i.indkey) " "LEFT JOIN pg_attrdef def ON def.adrelid = c.oid AND " "def.adnum = a.attnum " "LEFT JOIN spatial_ref_sys srs ON srs.srid = postgis_typmod_srid(a.atttypmod) " "ORDER BY a.attnum", OGRCARTODBEscapeLiteral(osName).c_str(), OGRCARTODBEscapeLiteral(poDS->GetCurrentSchema()).c_str()); } else if( poDS->HasOGRMetadataFunction() != FALSE ) { osCommand.Printf( "SELECT * FROM ogr_table_metadata('%s', '%s')", OGRCARTODBEscapeLiteral(poDS->GetCurrentSchema()).c_str(), OGRCARTODBEscapeLiteral(osName).c_str() ); } if( osCommand.size() ) { if( !poDS->IsAuthenticatedConnection() && poDS->HasOGRMetadataFunction() < 0 ) CPLPushErrorHandler(CPLQuietErrorHandler); OGRLayer* poLyr = poDS->ExecuteSQLInternal(osCommand); if( !poDS->IsAuthenticatedConnection() && poDS->HasOGRMetadataFunction() < 0 ) { CPLPopErrorHandler(); if( poLyr == NULL ) { CPLDebug("CARTODB", "ogr_table_metadata(text, text) not available"); CPLErrorReset(); } else if( poLyr->GetLayerDefn()->GetFieldCount() != 12 ) { CPLDebug("CARTODB", "ogr_table_metadata(text, text) has unexpected column count"); poDS->ReleaseResultSet(poLyr); poLyr = NULL; } poDS->SetOGRMetadataFunction(poLyr != NULL); } if( poLyr ) { OGRFeature* poFeat; while( (poFeat = poLyr->GetNextFeature()) != NULL ) { if( poFeatureDefn == NULL ) { // We could do that outside of the while() loop, but // by doing that here, we are somewhat robust to // ogr_table_metadata() returning suddenly an empty result set // for example if CDB_UserTables() no longer works poFeatureDefn = new OGRFeatureDefn(osName); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType(wkbNone); } const char* pszAttname = poFeat->GetFieldAsString("attname"); const char* pszType = poFeat->GetFieldAsString("typname"); int nWidth = poFeat->GetFieldAsInteger("attlen"); const char* pszFormatType = poFeat->GetFieldAsString("format_type"); int bNotNull = poFeat->GetFieldAsInteger("attnotnull"); int bIsPrimary = poFeat->GetFieldAsInteger("indisprimary"); int iDefaultExpr = poLyr->GetLayerDefn()->GetFieldIndex("defaultexpr"); const char* pszDefault = (iDefaultExpr >= 0 && poFeat->IsFieldSet(iDefaultExpr)) ? poFeat->GetFieldAsString(iDefaultExpr) : NULL; if( bIsPrimary && (EQUAL(pszType, "int2") || EQUAL(pszType, "int4") || EQUAL(pszType, "int8") || EQUAL(pszType, "serial") || EQUAL(pszType, "bigserial")) ) { osFIDColName = pszAttname; } else if( strcmp(pszAttname, "created_at") == 0 || strcmp(pszAttname, "updated_at") == 0 || strcmp(pszAttname, "the_geom_webmercator") == 0) { /* ignored */ } else { if( EQUAL(pszType,"geometry") ) { int nDim = poFeat->GetFieldAsInteger("dim"); int nSRID = poFeat->GetFieldAsInteger("srid"); const char* pszGeomType = poFeat->GetFieldAsString("geomtyp"); const char* pszSRText = (poFeat->IsFieldSet( poLyr->GetLayerDefn()->GetFieldIndex("srtext"))) ? poFeat->GetFieldAsString("srtext") : NULL; OGRwkbGeometryType eType = OGRFromOGCGeomType(pszGeomType); if( nDim == 3 ) eType = wkbSetZ(eType); OGRCartoDBGeomFieldDefn *poFieldDefn = new OGRCartoDBGeomFieldDefn(pszAttname, eType); if( bNotNull ) poFieldDefn->SetNullable(FALSE); OGRSpatialReference* poSRS = NULL; if( pszSRText != NULL ) { poSRS = new OGRSpatialReference(); char* pszTmp = (char* )pszSRText; if( poSRS->importFromWkt(&pszTmp) != OGRERR_NONE ) { delete poSRS; poSRS = NULL; } if( poSRS != NULL ) { poFieldDefn->SetSpatialRef(poSRS); poSRS->Release(); } } poFieldDefn->nSRID = nSRID; poFeatureDefn->AddGeomFieldDefn(poFieldDefn, FALSE); } else { OGRFieldDefn oField(pszAttname, OFTString); if( bNotNull ) oField.SetNullable(FALSE); OGRPGCommonLayerSetType(oField, pszType, pszFormatType, nWidth); if( pszDefault ) OGRPGCommonLayerNormalizeDefault(&oField, pszDefault); poFeatureDefn->AddFieldDefn( &oField ); } } delete poFeat; } poDS->ReleaseResultSet(poLyr); } } if( poFeatureDefn == NULL ) { osBaseSQL.Printf("SELECT * FROM %s", OGRCARTODBEscapeIdentifier(osName).c_str()); EstablishLayerDefn(osName, NULL); osBaseSQL = ""; } if( osFIDColName.size() > 0 ) { osBaseSQL = "SELECT "; osBaseSQL += OGRCARTODBEscapeIdentifier(osFIDColName); } for(int i=0; i<poFeatureDefn->GetGeomFieldCount(); i++) { if( osBaseSQL.size() == 0 ) osBaseSQL = "SELECT "; else osBaseSQL += ", "; osBaseSQL += OGRCARTODBEscapeIdentifier(poFeatureDefn->GetGeomFieldDefn(i)->GetNameRef()); } for(int i=0; i<poFeatureDefn->GetFieldCount(); i++) { if( osBaseSQL.size() == 0 ) osBaseSQL = "SELECT "; else osBaseSQL += ", "; osBaseSQL += OGRCARTODBEscapeIdentifier(poFeatureDefn->GetFieldDefn(i)->GetNameRef()); } if( osBaseSQL.size() == 0 ) osBaseSQL = "SELECT *"; osBaseSQL += " FROM "; osBaseSQL += OGRCARTODBEscapeIdentifier(osName); osSELECTWithoutWHERE = osBaseSQL; return poFeatureDefn; }
SEXP ogrReadListColumn(OGRLayer *poLayer, SEXP FIDs, int iField, int k, int int64) { // read feature data and return something according to the type OGRFeatureDefn *poDefn; OGRFieldDefn *poField; OGRFeature *poFeature; int iRow,nRows,nlist; SEXP ans = R_NilValue; nRows=length(FIDs); // get field data from layer installErrorHandler(); poDefn = poLayer->GetLayerDefn(); poField = poDefn->GetFieldDefn(iField); uninstallErrorHandlerAndTriggerError(); if(poField == NULL) { error("Error getting field %d ",iField); } // allocate an object for the result depending on the feature type: installErrorHandler(); switch(poField->GetType()) { case OFTIntegerList: PROTECT(ans=allocVector(INTSXP,nRows)); break; #ifdef GDALV2 case OFTInteger64List: if (int64 == 3) { PROTECT(ans=allocVector(STRSXP,nRows)); } else { PROTECT(ans=allocVector(INTSXP,nRows)); } break; #endif case OFTRealList: PROTECT(ans=allocVector(REALSXP,nRows)); break; case OFTStringList: PROTECT(ans=allocVector(STRSXP,nRows)); break; default: const char *desc = poField->GetFieldTypeName(poField->GetType()); uninstallErrorHandlerAndTriggerError(); error("unsupported field type: %s", desc); break; } uninstallErrorHandlerAndTriggerError(); // now go over each row and retrieve data. iRow is an index in a // vector of FIDs installErrorHandler(); poLayer->ResetReading(); OGRField* psField; iRow = 0; while((poFeature = poLayer->GetNextFeature()) != NULL) { if (poFeature->IsFieldSet(iField)) { // now get the value using the right type: psField = poFeature->GetRawFieldRef(iField); switch(poField->GetType()) { case OFTIntegerList: nlist = psField->IntegerList.nCount; if (k < nlist) INTEGER(ans)[iRow] = psField->IntegerList.paList[k]; else INTEGER(ans)[iRow]=NA_INTEGER; break; #ifdef GDALV2 case OFTInteger64List: nlist = psField->Integer64List.nCount; if (k < nlist) { if (int64 == 3) { // FIXME clang++ // GIntBig nVal64 = psField->Integer64List.paList[k]; char szItem[32]; snprintf(szItem, sizeof(szItem), CPL_FRMT_GIB, psField->Integer64List.paList[k]); SET_STRING_ELT(ans, iRow, mkChar(szItem)); } else { GIntBig nVal64 = psField->Integer64List.paList[k]; int nVal = (nVal64 > INT_MAX) ? INT_MAX : (nVal64 < INT_MIN) ? INT_MIN : (int) nVal64; if (((GIntBig)nVal != nVal64) && int64 == 2) { warning("Integer64 value clamped: feature %d", iRow); } INTEGER(ans)[iRow]=nVal; } } else { if (int64 == 3) { SET_STRING_ELT(ans, iRow, NA_STRING); } else { INTEGER(ans)[iRow]=NA_INTEGER; } } break; #endif case OFTRealList: nlist = psField->RealList.nCount; if (k < nlist) REAL(ans)[iRow] = psField->RealList.paList[k]; else REAL(ans)[iRow]=NA_REAL; break; case OFTStringList: nlist = psField->StringList.nCount; if (k < nlist) SET_STRING_ELT(ans,iRow,mkChar(psField->StringList.paList[k])); else SET_STRING_ELT(ans, iRow, NA_STRING); break; default: OGRFeature::DestroyFeature( poFeature ); // delete poFeature; uninstallErrorHandlerAndTriggerError(); error("Unsupported field type. should have been caught before"); } } OGRFeature::DestroyFeature( poFeature ); // delete poFeature; iRow++; } uninstallErrorHandlerAndTriggerError(); UNPROTECT(1); return(ans); }
SEXP ogrReadColumn(OGRLayer *poLayer, SEXP FIDs, int iField, int int64, int ENC_DEBUG) { // read feature data and return something according to the type OGRFeatureDefn *poDefn; OGRFieldDefn *poField; OGRFeature *poFeature; int iRow,nRows; SEXP ans = R_NilValue; nRows=length(FIDs); // get field data from layer installErrorHandler(); poDefn = poLayer->GetLayerDefn(); poField = poDefn->GetFieldDefn(iField); uninstallErrorHandlerAndTriggerError(); if(poField == NULL) { error("Error getting field %d ",iField); } // allocate an object for the result depending on the feature type: installErrorHandler(); switch(poField->GetType()) { case OFTInteger: PROTECT(ans=allocVector(INTSXP,nRows)); break; #ifdef GDALV2 case OFTInteger64: if (int64 ==3) { PROTECT(ans=allocVector(STRSXP,nRows)); } else { PROTECT(ans=allocVector(INTSXP,nRows)); } break; #endif case OFTReal: PROTECT(ans=allocVector(REALSXP,nRows)); break; case OFTString: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTDate: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTDateTime: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTTime: PROTECT(ans=allocVector(STRSXP,nRows)); break; default: const char *desc = poField->GetFieldTypeName(poField->GetType()); uninstallErrorHandlerAndTriggerError(); error("unsupported field type: %s", desc); break; } uninstallErrorHandlerAndTriggerError(); // now go over each row and retrieve data. iRow is an index in a // vector of FIDs /*#ifndef EJP installErrorHandler(); for(iRow=0;iRow<nRows;iRow++){ poFeature=poLayer->GetFeature(INTEGER(FIDs)[iRow]); if(poFeature == NULL){ error("Error getting feature FID: %d",(INTEGER(FIDs)[iRow])); } } uninstallErrorHandlerAndTriggerError(); #else*/ // EJP, changed into: installErrorHandler(); poLayer->ResetReading(); iRow = 0; while((poFeature = poLayer->GetNextFeature()) != NULL) { //#endif // now get the value using the right type: switch(poField->GetType()) { case OFTInteger: if (poFeature->IsFieldSet(iField)) INTEGER(ans)[iRow]=poFeature->GetFieldAsInteger(iField); else INTEGER(ans)[iRow]=NA_INTEGER; break; #ifdef GDALV2 case OFTInteger64: if (poFeature->IsFieldSet(iField)) { if (int64 == 3) { SET_STRING_ELT(ans, iRow, mkChar(poFeature->GetFieldAsString(iField))); } else { GIntBig nVal64 = poFeature->GetFieldAsInteger64(iField); int nVal = (nVal64 > INT_MAX) ? INT_MAX : (nVal64 < INT_MIN) ? INT_MIN : (int) nVal64; INTEGER(ans)[iRow]=nVal; if (((GIntBig)nVal != nVal64) && int64 == 2) { warning("Integer64 value clamped: feature %d", iRow); } } } else { if (int64 == 3) { SET_STRING_ELT(ans, iRow, NA_STRING); } else { INTEGER(ans)[iRow]=NA_INTEGER; } } break; #endif case OFTReal: if (poFeature->IsFieldSet(iField)) REAL(ans)[iRow]=poFeature->GetFieldAsDouble(iField); else REAL(ans)[iRow]=NA_REAL; break; case OFTString: // ENC char str[4096]; size_t stln; if (poFeature->IsFieldSet(iField)) { stln = CPLStrnlen(poFeature->GetFieldAsString(iField), 4096); CPLStrlcpy(str, (const char *) poFeature->GetFieldAsString(iField), 4096); SET_STRING_ELT(ans, iRow, mkChar(str)); } else SET_STRING_ELT(ans, iRow, NA_STRING); if (ENC_DEBUG) { Rprintf("iField: %d, iRow: %d stln %u Enc %s ", iField, iRow, stln, CPLIsUTF8(str, (int) stln)?"UTF-8":"other"); for (int si=0; si < (int) stln; si++) Rprintf("%x ", (unsigned char) str[si]); Rprintf("\n"); } /* FIXME */ break; case OFTDate: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; case OFTDateTime: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; case OFTTime: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; default: OGRFeature::DestroyFeature( poFeature ); // delete poFeature; uninstallErrorHandlerAndTriggerError(); error("Unsupported field type. should have been caught before"); } OGRFeature::DestroyFeature( poFeature ); // delete poFeature; //#ifdef EJP // according to tutorial: OGRFeature::DestroyFeature(poFeature); // see comment FW in OGR tutorial: We could just "delete" it, // but this can cause problems in windows builds where the GDAL DLL // has a different "heap" from the main program. To be on the safe // side we use a GDAL function to delete the feature. iRow++; //#endif } uninstallErrorHandlerAndTriggerError(); UNPROTECT(1); return(ans); }
GDALDataset* OGRPLScenesDataset::OpenRasterScene(GDALOpenInfo* poOpenInfo, CPLString osScene, char** papszOptions) { if( !(poOpenInfo->nOpenFlags & GDAL_OF_RASTER) ) { return NULL; } for( char** papszIter = papszOptions; papszIter && *papszIter; papszIter ++ ) { char* pszKey; const char* pszValue = CPLParseNameValue(*papszIter, &pszKey); if( pszValue != NULL ) { if( !EQUAL(pszKey, "api_key") && !EQUAL(pszKey, "scene") && !EQUAL(pszKey, "product_type") ) { CPLError(CE_Failure, CPLE_NotSupported, "Unsupported option %s", pszKey); CPLFree(pszKey); return NULL; } CPLFree(pszKey); } } const char* pszProductType = CSLFetchNameValueDef(papszOptions, "product_type", CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "PRODUCT_TYPE", "visual")); CPLString osRasterURL; osRasterURL = osBaseURL; osRasterURL += "ortho/"; osRasterURL += osScene; json_object* poObj = RunRequest( osRasterURL ); if( poObj == NULL ) return NULL; json_object* poProperties = json_object_object_get(poObj, "properties"); if( poProperties == NULL || json_object_get_type(poProperties) != json_type_object ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot find properties object"); json_object_put(poObj); return NULL; } const char* pszLink = NULL; if( EQUAL(pszProductType, "thumb") ) { json_object* poLinks = json_object_object_get(poProperties, "links"); if( poLinks != NULL && json_object_get_type(poLinks) == json_type_object ) { json_object* poThumbnail = json_object_object_get(poLinks, "thumbnail"); if( poThumbnail && json_object_get_type(poThumbnail) == json_type_string ) pszLink = json_object_get_string(poThumbnail); } } else { json_object* poData = json_object_object_get(poProperties, "data"); if( poData != NULL && json_object_get_type(poData) == json_type_object ) { json_object* poProducts = json_object_object_get(poData, "products"); if( poProducts != NULL && json_object_get_type(poProducts) == json_type_object ) { json_object* poProduct = json_object_object_get(poProducts, pszProductType); if( poProduct != NULL && json_object_get_type(poProduct) == json_type_object ) { json_object* poFull = json_object_object_get(poProduct, "full"); if( poFull && json_object_get_type(poFull) == json_type_string ) pszLink = json_object_get_string(poFull); } } } } osRasterURL = pszLink ? pszLink : ""; json_object_put(poObj); if( osRasterURL.size() == 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot find link to scene %s", osScene.c_str()); return NULL; } if( strncmp(osRasterURL, "http://", strlen("http://")) == 0 ) { osRasterURL = "http://" + osAPIKey + ":@" + osRasterURL.substr(strlen("http://")); } else if( strncmp(osRasterURL, "https://", strlen("https://")) == 0 ) { osRasterURL = "https://" + osAPIKey + ":@" + osRasterURL.substr(strlen("https://")); } CPLString osOldHead(CPLGetConfigOption("CPL_VSIL_CURL_USE_HEAD", "")); CPLString osOldExt(CPLGetConfigOption("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", "")); int bUseVSICURL = CSLFetchBoolean(poOpenInfo->papszOpenOptions, "RANDOM_ACCESS", TRUE); if( bUseVSICURL && !(strncmp(osBaseURL, "/vsimem/", strlen("/vsimem/")) == 0) ) { CPLSetThreadLocalConfigOption("CPL_VSIL_CURL_USE_HEAD", "NO"); CPLSetThreadLocalConfigOption("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", "{noext}"); VSIStatBufL sStat; if( VSIStatL(("/vsicurl/" + osRasterURL).c_str(), &sStat) == 0 && sStat.st_size > 0 ) { osRasterURL = "/vsicurl/" + osRasterURL; } else { CPLDebug("PLSCENES", "Cannot use random access for that file"); } } GDALDataset* poOutDS = (GDALDataset*) GDALOpen(osRasterURL, GA_ReadOnly); if( poOutDS ) { poOutDS->SetDescription(poOpenInfo->pszFilename); poOutDS->GetFileList(); /* so as to probe all auxiliary files before reseting the allowed extensions */ if( !EQUAL(pszProductType, "thumb") ) { OGRPLScenesLayer* poLayer = new OGRPLScenesLayer(this, "ortho", (osBaseURL + "ortho/").c_str()); papoLayers = (OGRPLScenesLayer**) CPLRealloc(papoLayers, sizeof(OGRPLScenesLayer*) * (nLayers + 1)); papoLayers[nLayers ++] = poLayer; /* Attach scene matadata */ poLayer->SetAttributeFilter(CPLSPrintf("id = '%s'", osScene.c_str())); OGRFeature* poFeat = poLayer->GetNextFeature(); if( poFeat ) { for(int i=0;i<poFeat->GetFieldCount();i++) { if( poFeat->IsFieldSet(i) ) { const char* pszKey = poFeat->GetFieldDefnRef(i)->GetNameRef(); const char* pszVal = poFeat->GetFieldAsString(i); if( strstr(pszKey, "file_size") == NULL && strstr(pszVal, "https://") == NULL ) { poOutDS->SetMetadataItem(pszKey, pszVal); } } } } delete poFeat; } } if( bUseVSICURL ) { CPLSetThreadLocalConfigOption("CPL_VSIL_CURL_USE_HEAD", osOldHead.size() ? osOldHead.c_str(): NULL); CPLSetThreadLocalConfigOption("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", osOldExt.size() ? osOldExt.c_str(): NULL); } return poOutDS; }