OGRFeature* OGRGeoJSONLayer::GetNextFeature() { while ( iterCurrent_ != seqFeatures_.end() ) { OGRFeature* poFeature = (*iterCurrent_); CPLAssert( NULL != poFeature ); ++iterCurrent_; if((m_poFilterGeom == NULL || FilterGeometry( poFeature->GetGeometryRef() ) ) && (m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeature )) ) { OGRFeature* poFeatureCopy = poFeature->Clone(); CPLAssert( NULL != poFeatureCopy ); if (poFeatureCopy->GetGeometryRef() != NULL && GetSpatialRef() != NULL) { poFeatureCopy->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); } return poFeatureCopy; } } return NULL; }
/********************************************************************** * IMapInfoFile::GetNextFeature() * * Standard OGR GetNextFeature implementation. This method is used * to retrieve the next OGRFeature. **********************************************************************/ OGRFeature *IMapInfoFile::GetNextFeature() { OGRFeature *poFeatureRef; OGRGeometry *poGeom; GIntBig nFeatureId; while( (nFeatureId = GetNextFeatureId(m_nCurFeatureId)) != -1 ) { poFeatureRef = GetFeatureRef(nFeatureId); if (poFeatureRef == NULL) return NULL; else if( (m_poFilterGeom == NULL || ((poGeom = poFeatureRef->GetGeometryRef()) != NULL && FilterGeometry( poGeom ))) && (m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeatureRef )) ) { // Avoid cloning feature... return the copy owned by the class CPLAssert(poFeatureRef == m_poCurFeature); m_poCurFeature = NULL; if( poFeatureRef->GetGeometryRef() != NULL ) poFeatureRef->GetGeometryRef()->assignSpatialReference(GetSpatialRef()); return poFeatureRef; } } return NULL; }
OGRFeature* OGRUnionLayer::TranslateFromSrcLayer(OGRFeature* poSrcFeature) { CPLAssert(panMap != NULL); CPLAssert(iCurLayer >= 0 && iCurLayer < nSrcLayers); OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFrom(poSrcFeature, panMap, TRUE); if( osSourceLayerFieldName.size() && !poFeatureDefn->GetFieldDefn(0)->IsIgnored() ) { poFeature->SetField(0, papoSrcLayers[iCurLayer]->GetName()); } if( poFeatureDefn->IsGeometryIgnored() ) poFeature->SetGeometryDirectly(NULL); else { OGRGeometry* poGeom = poFeature->GetGeometryRef(); if( poGeom != NULL ) poGeom->assignSpatialReference(GetSpatialRef()); } if( bPreserveSrcFID ) poFeature->SetFID(poSrcFeature->GetFID()); else poFeature->SetFID(nNextFID ++); return poFeature; }
OGRMSSQLSpatialSelectLayer::OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *poDSIn, CPLODBCStatement * poStmtIn ) { poDS = poDSIn; iNextShapeId = 0; nSRSId = -1; poFeatureDefn = NULL; poStmt = poStmtIn; pszBaseStatement = CPLStrdup( poStmtIn->GetCommand() ); /* identify the geometry column */ pszGeomColumn = NULL; for ( int iColumn = 0; iColumn < poStmt->GetColCount(); iColumn++ ) { if ( EQUAL(poStmt->GetColTypeName( iColumn ), "image") ) { SQLCHAR szTableName[256]; SQLSMALLINT nTableNameLength = 0; SQLColAttribute(poStmt->GetStatement(), (SQLSMALLINT)(iColumn + 1), SQL_DESC_TABLE_NAME, szTableName, sizeof(szTableName), &nTableNameLength, NULL); if (nTableNameLength > 0) { OGRLayer *poBaseLayer = poDS->GetLayerByName((const char*)szTableName); if (poBaseLayer != NULL && EQUAL(poBaseLayer->GetGeometryColumn(), poStmt->GetColName(iColumn))) { nGeomColumnType = MSSQLCOLTYPE_BINARY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); /* copy spatial reference */ if (!poSRS && poBaseLayer->GetSpatialRef()) poSRS = poBaseLayer->GetSpatialRef()->Clone(); break; } } } else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geometry") ) { nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); break; } else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geography") ) { nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); break; } } BuildFeatureDefn( "SELECT", poStmt ); if ( GetSpatialRef() && poFeatureDefn->GetGeomFieldCount() == 1) poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef( poSRS ); }
CPLErr OGRMSSQLSpatialTableLayer::Initialize( const char *pszSchema, const char *pszLayerName, const char *pszGeomCol, int nCoordDimension, int nSRId, OGRwkbGeometryType eType ) { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; /* -------------------------------------------------------------------- */ /* Parse out schema name if present in layer. We assume a */ /* schema is provided if there is a dot in the name, and that */ /* it is in the form <schema>.<tablename> */ /* -------------------------------------------------------------------- */ this->pszLayerName = CPLStrdup(pszLayerName); const char *pszDot = strstr(pszLayerName,"."); if( pszDot != NULL ) { pszTableName = CPLStrdup(pszDot + 1); pszSchemaName = CPLStrdup(pszLayerName); pszSchemaName[pszDot - pszLayerName] = '\0'; } else { pszTableName = CPLStrdup(pszLayerName); pszSchemaName = CPLStrdup(pszSchema); } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); eGeomType = eType; /* -------------------------------------------------------------------- */ /* Try to find out the spatial reference */ /* -------------------------------------------------------------------- */ nSRSId = nSRId; if (nSRSId < 0) nSRSId = FetchSRSId(); GetSpatialRef(); return CE_None; }
int OGRGeomFieldDefn::IsSame( OGRGeomFieldDefn * poOtherFieldDefn ) { if( !(strcmp(GetNameRef(), poOtherFieldDefn->GetNameRef()) == 0 && GetType() == poOtherFieldDefn->GetType() && IsNullable() == poOtherFieldDefn->IsNullable()) ) return FALSE; OGRSpatialReference* poMySRS = GetSpatialRef(); OGRSpatialReference* poOtherSRS = poOtherFieldDefn->GetSpatialRef(); return ((poMySRS == poOtherSRS) || (poMySRS != NULL && poOtherSRS != NULL && poMySRS->IsSame(poOtherSRS))); }
void OGRMySQLTableLayer::BuildWhere() { CPLFree( pszWHERE ); const size_t nWHERELen = 500 + ((pszQuery) ? strlen(pszQuery) : 0); pszWHERE = (char*)CPLMalloc(nWHERELen); pszWHERE[0] = '\0'; if( m_poFilterGeom != nullptr && pszGeomColumn ) { char szEnvelope[400]; OGREnvelope sEnvelope; szEnvelope[0] = '\0'; //POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY)) m_poFilterGeom->getEnvelope( &sEnvelope ); CPLsnprintf(szEnvelope, sizeof(szEnvelope), "POLYGON((%.18g %.18g, %.18g %.18g, %.18g %.18g, %.18g %.18g, %.18g %.18g))", sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MaxY, sEnvelope.MinX, sEnvelope.MaxY, sEnvelope.MinX, sEnvelope.MinY); const char* pszAxisOrder = ""; OGRSpatialReference* l_poSRS = GetSpatialRef(); if( poDS->GetMajorVersion() >= 8 && !poDS->IsMariaDB() && l_poSRS && l_poSRS->IsGeographic() ) { pszAxisOrder = ", 'axis-order=long-lat'"; } snprintf( pszWHERE, nWHERELen, "WHERE MBRIntersects(%s('%s', %d%s), `%s`)", poDS->GetMajorVersion() >= 8 ? "ST_GeomFromText" : "GeomFromText", szEnvelope, nSRSId, pszAxisOrder, pszGeomColumn); } if( pszQuery != nullptr ) { if( strlen(pszWHERE) == 0 ) snprintf( pszWHERE, nWHERELen, "WHERE %s ", pszQuery ); else snprintf( pszWHERE+strlen(pszWHERE), nWHERELen - strlen(pszWHERE), "&& (%s) ", pszQuery ); } }
OGRFeature *OGRS57Layer::GetNextUnfilteredFeature() { OGRFeature *poFeature = NULL; /* -------------------------------------------------------------------- */ /* Are we out of modules to request features from? */ /* -------------------------------------------------------------------- */ if( nCurrentModule >= poDS->GetModuleCount() ) return NULL; /* -------------------------------------------------------------------- */ /* Set the current position on the current module and fetch a */ /* feature. */ /* -------------------------------------------------------------------- */ S57Reader *poReader = poDS->GetModule(nCurrentModule); if( poReader != NULL ) { poReader->SetNextFEIndex( nNextFEIndex, nRCNM ); poFeature = poReader->ReadNextFeature( poFeatureDefn ); nNextFEIndex = poReader->GetNextFEIndex( nRCNM ); } /* -------------------------------------------------------------------- */ /* If we didn't get a feature we need to move onto the next file. */ /* -------------------------------------------------------------------- */ if( poFeature == NULL ) { nCurrentModule++; poReader = poDS->GetModule(nCurrentModule); if( poReader != NULL && poReader->GetModule() == NULL ) { if( !poReader->Open( FALSE ) ) return NULL; } return GetNextUnfilteredFeature(); } else { m_nFeaturesRead++; if( poFeature->GetGeometryRef() != NULL ) poFeature->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); } return poFeature; }
OGRFeature* OGRGeoJSONSeqLayer::GetNextFeature() { while( true ) { auto poObject = GetNextObject(); if( !poObject ) return nullptr; OGRFeature* poFeature; auto type = OGRGeoJSONGetType(poObject); if( type == GeoJSONObject::eFeature ) { poFeature = m_oReader.ReadFeature( this, poObject, m_osFeatureBuffer.c_str() ); json_object_put(poObject); } else if( type == GeoJSONObject::eFeatureCollection || type == GeoJSONObject::eUnknown ) { json_object_put(poObject); continue; } else { OGRGeometry* poGeom = m_oReader.ReadGeometry(poObject, GetSpatialRef()); json_object_put(poObject); if( !poGeom ) { continue; } poFeature = new OGRFeature(m_poFeatureDefn); poFeature->SetGeometryDirectly(poGeom); } if( poFeature->GetFID() == OGRNullFID ) { poFeature->SetFID(m_nNextFID); m_nNextFID ++; } if( (m_poFilterGeom == nullptr || FilterGeometry(poFeature->GetGeomFieldRef(m_iGeomFieldFilter)) ) && (m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature)) ) { return poFeature; } delete poFeature; } }
OGRFeature *OGRS57Layer::GetFeature( long nFeatureId ) { S57Reader *poReader = poDS->GetModule(0); // not multi-reader aware if( poReader != NULL ) { OGRFeature *poFeature; poFeature = poReader->ReadFeature( nFeatureId, poFeatureDefn ); if( poFeature != NULL && poFeature->GetGeometryRef() != NULL ) poFeature->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); return poFeature; } else return NULL; }
OGRFeature *OGRS57Layer::GetFeature( GIntBig nFeatureId ) { S57Reader *poReader = poDS->GetModule(0); // not multi-reader aware if( poReader != NULL && nFeatureId <= INT_MAX ) { OGRFeature *poFeature = poReader->ReadFeature( static_cast<int>(nFeatureId), poFeatureDefn ); if( poFeature != NULL && poFeature->GetGeometryRef() != NULL ) poFeature->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); return poFeature; } return NULL; }
void OGRGeoconceptLayer::SetSpatialRef( OGRSpatialReference *poSpatialRef ) { GCSysCoord* os, *ns; OGRSpatialReference* poSRS= GetSpatialRef(); GCExportFileH* hGXT; GCExportFileMetadata* Meta; /*----------------------------------------------------------------- * Keep a copy of the OGRSpatialReference... * Note: we have to take the reference count into account... *----------------------------------------------------------------*/ if( poSRS && poSRS->Dereference() == 0) delete poSRS; if( !poSpatialRef ) return; poSRS= poSpatialRef->Clone(); if( !(hGXT= GetSubTypeGCHandle_GCIO(_gcFeature)) ) return; if( !(Meta= GetGCMeta_GCIO(hGXT)) ) return; os= GetMetaSysCoord_GCIO(Meta); ns= OGRSpatialReference2SysCoord_GCSRS((OGRSpatialReferenceH)poSRS); if( os && ns && GetSysCoordSystemID_GCSRS(os)!=-1 && ( GetSysCoordSystemID_GCSRS(os)!=GetSysCoordSystemID_GCSRS(ns) || GetSysCoordTimeZone_GCSRS(os)!=GetSysCoordTimeZone_GCSRS(ns) ) ) { CPLError( CE_Warning, CPLE_AppDefined, "Can't change SRS on Geoconcept layers.\n" ); return; } if( os ) DestroySysCoord_GCSRS(&os); SetMetaSysCoord_GCIO(Meta, ns); SetMetaSRS_GCIO(Meta, (OGRSpatialReferenceH)poSRS); return; }
void OGRUnionLayer::AutoWarpLayerIfNecessary(int iLayer) { if( !pabCheckIfAutoWrap[iLayer] ) { pabCheckIfAutoWrap[iLayer] = TRUE; OGRSpatialReference* poSRS = GetSpatialRef(); if( poSRS != NULL ) poSRS->Reference(); OGRSpatialReference* poSRS2 = papoSrcLayers[iLayer]->GetSpatialRef(); if( (poSRS == NULL && poSRS2 != NULL) || (poSRS != NULL && poSRS2 == NULL) ) { CPLError(CE_Warning, CPLE_AppDefined, "SRS of layer %s not consistant with layer SRS", papoSrcLayers[iLayer]->GetName()); } else if (poSRS != NULL && poSRS2 != NULL && poSRS != poSRS2 && !poSRS->IsSame(poSRS2)) { CPLDebug("VRT", "SRS of layer %s not consistant with layer SRS. " "Trying auto warping", papoSrcLayers[iLayer]->GetName()); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation( poSRS2, poSRS ); OGRCoordinateTransformation* poReversedCT = (poCT != NULL) ? OGRCreateCoordinateTransformation( poSRS, poSRS2 ) : NULL; if( poCT != NULL && poReversedCT != NULL ) papoSrcLayers[iLayer] = new OGRWarpedLayer( papoSrcLayers[iLayer], TRUE, poCT, poReversedCT); } if( poSRS != NULL ) poSRS->Release(); } }
OGRFeature *OGRShapeLayer::GetFeature( long nFeatureId ) { OGRFeature *poFeature = NULL; poFeature = SHPReadOGRFeature( hSHP, hDBF, poFeatureDefn, nFeatureId, NULL); if( poFeature != NULL ) { if( poFeature->GetGeometryRef() != NULL ) { poFeature->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); } m_nFeaturesRead++; return poFeature; } /* * Reading shape feature failed. */ return NULL; }
OGRFeature *OGRShapeLayer::GetNextFeature() { OGRFeature *poFeature = NULL; /* -------------------------------------------------------------------- */ /* Collect a matching list if we have attribute or spatial */ /* indices. Only do this on the first request for a given pass */ /* of course. */ /* -------------------------------------------------------------------- */ if( (m_poAttrQuery != NULL || m_poFilterGeom != NULL) && iNextShapeId == 0 && panMatchingFIDs == NULL ) { ScanIndices(); } /* -------------------------------------------------------------------- */ /* Loop till we find a feature matching our criteria. */ /* -------------------------------------------------------------------- */ while( TRUE ) { if( panMatchingFIDs != NULL ) { if( panMatchingFIDs[iMatchingFID] == OGRNullFID ) { return NULL; } // Check the shape object's geometry, and if it matches // any spatial filter, return it. poFeature = FetchShape(panMatchingFIDs[iMatchingFID]); iMatchingFID++; } else { if( iNextShapeId >= nTotalShapeCount ) { return NULL; } if ( hDBF && DBFIsRecordDeleted( hDBF, iNextShapeId ) ) { poFeature = NULL; } else { // Check the shape object's geometry, and if it matches // any spatial filter, return it. poFeature = FetchShape(iNextShapeId); } iNextShapeId++; } if( poFeature != NULL ) { if( poFeature->GetGeometryRef() != NULL ) { poFeature->GetGeometryRef()->assignSpatialReference( GetSpatialRef() ); } m_nFeaturesRead++; if( (m_poFilterGeom == NULL || FilterGeometry( poFeature->GetGeometryRef() ) ) && (m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeature )) ) { return poFeature; } delete poFeature; } } /* * NEVER SHOULD GET HERE */ CPLAssert(!"OGRShapeLayer::GetNextFeature(): Execution never should get here!"); }
CPLXMLNode *OGRFMELayerCached::SerializeToXML() { CPLXMLNode *psLayer; char szGeomType[64]; psLayer = CPLCreateXMLNode( NULL, CXT_Element, "OGRLayer" ); /* -------------------------------------------------------------------- */ /* Handle various layer values. */ /* -------------------------------------------------------------------- */ CPLCreateXMLElementAndValue( psLayer, "Name", poFeatureDefn->GetName()); sprintf( szGeomType, "%d", (int) poFeatureDefn->GetGeomType() ); CPLCreateXMLElementAndValue( psLayer, "GeomType", szGeomType ); CPLCreateXMLElementAndValue( psLayer, "SpatialCacheName", pszIndexBase ); /* -------------------------------------------------------------------- */ /* Handle spatial reference if available. */ /* -------------------------------------------------------------------- */ if( GetSpatialRef() != NULL ) { char *pszWKT = NULL; OGRSpatialReference *poSRS = GetSpatialRef(); poSRS->exportToWkt( &pszWKT ); if( pszWKT != NULL ) { CPLCreateXMLElementAndValue( psLayer, "SRS", pszWKT ); CPLFree( pszWKT ); } } /* -------------------------------------------------------------------- */ /* Handle extents if available. */ /* -------------------------------------------------------------------- */ OGREnvelope sEnvelope; if( GetExtent( &sEnvelope, FALSE ) == OGRERR_NONE ) { char szExtent[512]; sprintf( szExtent, "%24.15E,%24.15E,%24.15E,%24.15E", sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MaxY ); CPLCreateXMLElementAndValue( psLayer, "Extent", szExtent ); } /* -------------------------------------------------------------------- */ /* Emit the field schemas. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psSchema = CPLCreateXMLNode( psLayer, CXT_Element, "Schema" ); for( int iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDef = poFeatureDefn->GetFieldDefn( iField ); const char *pszType; char szWidth[32], szPrecision[32]; CPLXMLNode *psXMLFD; sprintf( szWidth, "%d", poFieldDef->GetWidth() ); sprintf( szPrecision, "%d", poFieldDef->GetPrecision() ); if( poFieldDef->GetType() == OFTInteger ) pszType = "Integer"; else if( poFieldDef->GetType() == OFTIntegerList ) pszType = "IntegerList"; else if( poFieldDef->GetType() == OFTReal ) pszType = "Real"; else if( poFieldDef->GetType() == OFTRealList ) pszType = "RealList"; else if( poFieldDef->GetType() == OFTString ) pszType = "String"; else if( poFieldDef->GetType() == OFTStringList ) pszType = "StringList"; else if( poFieldDef->GetType() == OFTBinary ) pszType = "Binary"; else pszType = "Unsupported"; psXMLFD = CPLCreateXMLNode( psSchema, CXT_Element, "OGRFieldDefn" ); CPLCreateXMLElementAndValue( psXMLFD, "Name",poFieldDef->GetNameRef()); CPLCreateXMLElementAndValue( psXMLFD, "Type", pszType ); CPLCreateXMLElementAndValue( psXMLFD, "Width", szWidth ); CPLCreateXMLElementAndValue( psXMLFD, "Precision", szPrecision ); } return psLayer; }
OGRDB2SelectLayer::OGRDB2SelectLayer( OGRDB2DataSource *poDSIn, OGRDB2Statement * poStmtIn ) { SQLCHAR szTableName[256]; SQLCHAR szSchemaName[256]; SQLSMALLINT nNameLength = 0; OGRDB2Layer *poBaseLayer = NULL; poDS = poDSIn; iNextShapeId = 0; nSRSId = -1; poFeatureDefn = NULL; m_poStmt = poStmtIn; pszBaseStatement = CPLStrdup( poStmtIn->GetCommand() ); CPLDebug("OGR_DB2SelectLayer::OGRDB2SelectLayer", "SQL: '%s'", pszBaseStatement); pszGeomColumn = NULL; /* get schema and table names for first column, column 1 */ SQLColAttribute(m_poStmt->GetStatement(), (SQLSMALLINT)(1), SQL_DESC_SCHEMA_NAME, szSchemaName, sizeof(szSchemaName), &nNameLength, NULL); /* The schema name is sometimes right padded with blanks */ /* Replace blanks with nulls to terminate string for sprintf below */ for (int i = 0; i < nNameLength; i++) { if (szSchemaName[i] == ' ') szSchemaName[i] = 0; }; SQLColAttribute(m_poStmt->GetStatement(), (SQLSMALLINT)(1), SQL_DESC_TABLE_NAME, szTableName, sizeof(szTableName), &nNameLength, NULL); CPLDebug("OGR_DB2SelectLayer::OGRDB2SelectLayer", "szSchemaName: '%s'; szTableName: '%s'", szSchemaName, szTableName); if (nNameLength > 0) { char szLayerName[512]; sprintf(szLayerName, "%s.%s",szSchemaName, szTableName); poBaseLayer = (OGRDB2Layer *) poDS->GetLayerByName((const char*) szLayerName); if (poBaseLayer != NULL) CPLDebug("OGR_DB2SelectLayer::OGRDB2SelectLayer", "base geom col: '%s'", poBaseLayer->GetGeometryColumn()); else CPLDebug("OGR_DB2SelectLayer::OGRDB2SelectLayer", "base layer not found"); } /* identify the geometry column */ for ( int iColumn = 0; iColumn < m_poStmt->GetColCount(); iColumn++ ) { if ( EQUAL(m_poStmt->GetColTypeName( iColumn ), "CLOB") || EQUAL(m_poStmt->GetColTypeName( iColumn ), "VARCHAR () FOR BIT DATA")) { if (poBaseLayer != NULL && EQUAL(poBaseLayer->GetGeometryColumn(), m_poStmt->GetColName(iColumn))) { pszGeomColumn = CPLStrdup(m_poStmt->GetColName(iColumn)); /* copy spatial reference */ if (!poSRS && poBaseLayer->GetSpatialRef()) poSRS = poBaseLayer->GetSpatialRef()->Clone(); nSRSId = poBaseLayer->GetSRSId(); break; } } } BuildFeatureDefn( "SELECT", m_poStmt ); if ( GetSpatialRef() && poFeatureDefn->GetGeomFieldCount() == 1) poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef( poSRS ); }
void OGRPDFLayer::Fill( GDALPDFArray* poArray ) { for(int i=0;i<poArray->GetLength();i++) { GDALPDFObject* poFeatureObj = poArray->Get(i); if (poFeatureObj->GetType() != PDFObjectType_Dictionary) continue; GDALPDFObject* poA = poFeatureObj->GetDictionary()->Get("A"); if (!(poA != NULL && poA->GetType() == PDFObjectType_Dictionary)) continue; GDALPDFObject* poP = poA->GetDictionary()->Get("P"); if (!(poP != NULL && poP->GetType() == PDFObjectType_Array)) continue; GDALPDFObject* poK = poFeatureObj->GetDictionary()->Get("K"); int nK = -1; if (poK != NULL && poK->GetType() == PDFObjectType_Int) nK = poK->GetInt(); GDALPDFArray* poPArray = poP->GetArray(); int j; for(j = 0;j<poPArray->GetLength();j++) { GDALPDFObject* poKV = poPArray->Get(j); if (poKV->GetType() == PDFObjectType_Dictionary) { GDALPDFObject* poN = poKV->GetDictionary()->Get("N"); GDALPDFObject* poV = poKV->GetDictionary()->Get("V"); if (poN != NULL && poN->GetType() == PDFObjectType_String && poV != NULL) { int nIdx = GetLayerDefn()->GetFieldIndex( poN->GetString().c_str() ); OGRFieldType eType = OFTString; if (poV->GetType() == PDFObjectType_Int) eType = OFTInteger; else if (poV->GetType() == PDFObjectType_Real) eType = OFTReal; if (nIdx < 0) { OGRFieldDefn oField(poN->GetString().c_str(), eType); CreateField(&oField); } else if (GetLayerDefn()->GetFieldDefn(nIdx)->GetType() != eType && GetLayerDefn()->GetFieldDefn(nIdx)->GetType() != OFTString) { OGRFieldDefn oField(poN->GetString().c_str(), OFTString); AlterFieldDefn( nIdx, &oField, ALTER_TYPE_FLAG ); } } } } OGRFeature* poFeature = new OGRFeature(GetLayerDefn()); for(j = 0;j<poPArray->GetLength();j++) { GDALPDFObject* poKV = poPArray->Get(j); if (poKV->GetType() == PDFObjectType_Dictionary) { GDALPDFObject* poN = poKV->GetDictionary()->Get("N"); GDALPDFObject* poV = poKV->GetDictionary()->Get("V"); if (poN != NULL && poN->GetType() == PDFObjectType_String && poV != NULL) { if (poV->GetType() == PDFObjectType_String) poFeature->SetField(poN->GetString().c_str(), poV->GetString().c_str()); else if (poV->GetType() == PDFObjectType_Int) poFeature->SetField(poN->GetString().c_str(), poV->GetInt()); else if (poV->GetType() == PDFObjectType_Real) poFeature->SetField(poN->GetString().c_str(), poV->GetReal()); } } } if (nK >= 0) { OGRGeometry* poGeom = poDS->GetGeometryFromMCID(nK); if (poGeom) { poGeom->assignSpatialReference(GetSpatialRef()); poFeature->SetGeometry(poGeom); } } OGRGeometry* poGeom = poFeature->GetGeometryRef(); if( !bGeomTypeMixed && poGeom != NULL ) { if (!bGeomTypeSet) { bGeomTypeSet = TRUE; GetLayerDefn()->SetGeomType(poGeom->getGeometryType()); } else if (GetLayerDefn()->GetGeomType() != poGeom->getGeometryType()) { bGeomTypeMixed = TRUE; GetLayerDefn()->SetGeomType(wkbUnknown); } } ICreateFeature(poFeature); delete poFeature; } }
OGRFeature *OGRMySQLLayer::RecordToFeature( char **papszRow, unsigned long *panLengths ) { mysql_field_seek( hResultSet, 0 ); /* -------------------------------------------------------------------- */ /* Create a feature from the current result. */ /* -------------------------------------------------------------------- */ int iField; OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); poFeature->SetFID( iNextShapeId ); m_nFeaturesRead++; /* ==================================================================== */ /* Transfer all result fields we can. */ /* ==================================================================== */ for( iField = 0; iField < (int) mysql_num_fields(hResultSet); iField++ ) { int iOGRField; MYSQL_FIELD *psMSField = mysql_fetch_field(hResultSet); /* -------------------------------------------------------------------- */ /* Handle FID. */ /* -------------------------------------------------------------------- */ if( bHasFid && EQUAL(psMSField->name,pszFIDColumn) ) { if( papszRow[iField] == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "NULL primary key in RecordToFeature()" ); return NULL; } poFeature->SetFID( CPLAtoGIntBig(papszRow[iField]) ); } if( papszRow[iField] == NULL ) { // CPLDebug("MYSQL", "%s was null for %d", psMSField->name, // iNextShapeId); continue; } /* -------------------------------------------------------------------- */ /* Handle MySQL geometry */ /* -------------------------------------------------------------------- */ if( pszGeomColumn && EQUAL(psMSField->name,pszGeomColumn)) { OGRGeometry *poGeometry = NULL; // Geometry columns will have the first 4 bytes contain the SRID. OGRGeometryFactory::createFromWkb( ((GByte *)papszRow[iField]) + 4, NULL, &poGeometry, panLengths[iField] - 4 ); if( poGeometry != NULL ) { poGeometry->assignSpatialReference( GetSpatialRef() ); poFeature->SetGeometryDirectly( poGeometry ); } continue; } /* -------------------------------------------------------------------- */ /* Transfer regular data fields. */ /* -------------------------------------------------------------------- */ iOGRField = poFeatureDefn->GetFieldIndex(psMSField->name); if( iOGRField < 0 ) continue; OGRFieldDefn *psFieldDefn = poFeatureDefn->GetFieldDefn( iOGRField ); if( psFieldDefn->GetType() == OFTBinary ) { poFeature->SetField( iOGRField, panLengths[iField], (GByte *) papszRow[iField] ); } else { poFeature->SetField( iOGRField, papszRow[iField] ); } } return poFeature; }
OGRFeatureDefn *OGRUnionLayer::GetLayerDefn() { if( poFeatureDefn != NULL ) return poFeatureDefn; poFeatureDefn = new OGRFeatureDefn( osName ); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType(wkbNone); int iCompareFirstIndex = 0; if( osSourceLayerFieldName.size() ) { OGRFieldDefn oField(osSourceLayerFieldName, OFTString); poFeatureDefn->AddFieldDefn(&oField); iCompareFirstIndex = 1; } if( eFieldStrategy == FIELD_SPECIFIED ) { int i; for(i = 0; i < nFields; i++) poFeatureDefn->AddFieldDefn(papoFields[i]); for(i = 0; i < nGeomFields; i++) { poFeatureDefn->AddGeomFieldDefn(new OGRUnionLayerGeomFieldDefn(papoGeomFields[i]), FALSE); OGRUnionLayerGeomFieldDefn* poGeomFieldDefn = (OGRUnionLayerGeomFieldDefn* ) poFeatureDefn->GetGeomFieldDefn(i); if( poGeomFieldDefn->bGeomTypeSet == FALSE || poGeomFieldDefn->bSRSSet == FALSE ) { for(int iLayer = 0; iLayer < nSrcLayers; iLayer++) { OGRFeatureDefn* poSrcFeatureDefn = papoSrcLayers[iLayer]->GetLayerDefn(); int nIndex = poSrcFeatureDefn->GetGeomFieldIndex(poGeomFieldDefn->GetNameRef()); if( nIndex >= 0 ) { OGRGeomFieldDefn* poSrcGeomFieldDefn = poSrcFeatureDefn->GetGeomFieldDefn(nIndex); if( poGeomFieldDefn->bGeomTypeSet == FALSE ) { poGeomFieldDefn->bGeomTypeSet = TRUE; poGeomFieldDefn->SetType(poSrcGeomFieldDefn->GetType()); } if( poGeomFieldDefn->bSRSSet == FALSE ) { poGeomFieldDefn->bSRSSet = TRUE; poGeomFieldDefn->SetSpatialRef(poSrcGeomFieldDefn->GetSpatialRef()); if( i == 0 && poGlobalSRS == NULL ) { poGlobalSRS = poSrcGeomFieldDefn->GetSpatialRef(); if( poGlobalSRS != NULL ) poGlobalSRS->Reference(); } } break; } } } } } else if( eFieldStrategy == FIELD_FROM_FIRST_LAYER ) { OGRFeatureDefn* poSrcFeatureDefn = papoSrcLayers[0]->GetLayerDefn(); int i; for(i = 0; i < poSrcFeatureDefn->GetFieldCount(); i++) poFeatureDefn->AddFieldDefn(poSrcFeatureDefn->GetFieldDefn(i)); for(i = 0; nGeomFields != - 1 && i < poSrcFeatureDefn->GetGeomFieldCount(); i++) { OGRGeomFieldDefn* poFldDefn = poSrcFeatureDefn->GetGeomFieldDefn(i); poFeatureDefn->AddGeomFieldDefn( new OGRUnionLayerGeomFieldDefn(poFldDefn), FALSE); } } else if (eFieldStrategy == FIELD_UNION_ALL_LAYERS ) { if( nGeomFields == 1 ) { poFeatureDefn->AddGeomFieldDefn( new OGRUnionLayerGeomFieldDefn(papoGeomFields[0]), FALSE); } for(int iLayer = 0; iLayer < nSrcLayers; iLayer++) { OGRFeatureDefn* poSrcFeatureDefn = papoSrcLayers[iLayer]->GetLayerDefn(); /* Add any field that is found in the source layers */ int i; for(i = 0; i < poSrcFeatureDefn->GetFieldCount(); i++) { OGRFieldDefn* poSrcFieldDefn = poSrcFeatureDefn->GetFieldDefn(i); int nIndex = poFeatureDefn->GetFieldIndex(poSrcFieldDefn->GetNameRef()); if( nIndex < 0 ) poFeatureDefn->AddFieldDefn(poSrcFieldDefn); else { OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(nIndex); MergeFieldDefn(poFieldDefn, poSrcFieldDefn); } } for(i = 0; nGeomFields != - 1 && i < poSrcFeatureDefn->GetGeomFieldCount(); i++) { OGRGeomFieldDefn* poSrcFieldDefn = poSrcFeatureDefn->GetGeomFieldDefn(i); int nIndex = poFeatureDefn->GetGeomFieldIndex(poSrcFieldDefn->GetNameRef()); if( nIndex < 0 ) { poFeatureDefn->AddGeomFieldDefn( new OGRUnionLayerGeomFieldDefn(poSrcFieldDefn), FALSE); if( poFeatureDefn->GetGeomFieldCount() == 1 && nGeomFields == 0 && GetSpatialRef() != NULL ) { OGRUnionLayerGeomFieldDefn* poGeomFieldDefn = (OGRUnionLayerGeomFieldDefn* ) poFeatureDefn->GetGeomFieldDefn(0); poGeomFieldDefn->bSRSSet = TRUE; poGeomFieldDefn->SetSpatialRef(GetSpatialRef()); } } else { if( nIndex == 0 && nGeomFields == 1 ) { OGRUnionLayerGeomFieldDefn* poGeomFieldDefn = (OGRUnionLayerGeomFieldDefn* ) poFeatureDefn->GetGeomFieldDefn(0); if( poGeomFieldDefn->bGeomTypeSet == FALSE ) { poGeomFieldDefn->bGeomTypeSet = TRUE; poGeomFieldDefn->SetType(poSrcFieldDefn->GetType()); } if( poGeomFieldDefn->bSRSSet == FALSE ) { poGeomFieldDefn->bSRSSet = TRUE; poGeomFieldDefn->SetSpatialRef(poSrcFieldDefn->GetSpatialRef()); } } /* TODO: merge type, SRS, extent ? */ } } } } else if (eFieldStrategy == FIELD_INTERSECTION_ALL_LAYERS ) { OGRFeatureDefn* poSrcFeatureDefn = papoSrcLayers[0]->GetLayerDefn(); int i; for(i = 0; i < poSrcFeatureDefn->GetFieldCount(); i++) poFeatureDefn->AddFieldDefn(poSrcFeatureDefn->GetFieldDefn(i)); for(i = 0; i < poSrcFeatureDefn->GetGeomFieldCount(); i++) { OGRGeomFieldDefn* poFldDefn = poSrcFeatureDefn->GetGeomFieldDefn(i); poFeatureDefn->AddGeomFieldDefn( new OGRUnionLayerGeomFieldDefn(poFldDefn), FALSE); } /* Remove any field that is not found in the source layers */ for(int iLayer = 1; iLayer < nSrcLayers; iLayer++) { OGRFeatureDefn* l_poSrcFeatureDefn = papoSrcLayers[iLayer]->GetLayerDefn(); for(i = iCompareFirstIndex; i < poFeatureDefn->GetFieldCount();) { OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(i); int nSrcIndex = l_poSrcFeatureDefn->GetFieldIndex( poFieldDefn->GetNameRef()); if( nSrcIndex < 0 ) { poFeatureDefn->DeleteFieldDefn(i); } else { OGRFieldDefn* poSrcFieldDefn = l_poSrcFeatureDefn->GetFieldDefn(nSrcIndex); MergeFieldDefn(poFieldDefn, poSrcFieldDefn); i ++; } } for(i = 0; i < poFeatureDefn->GetGeomFieldCount();) { OGRGeomFieldDefn* poFieldDefn = poFeatureDefn->GetGeomFieldDefn(i); int nSrcIndex = l_poSrcFeatureDefn->GetGeomFieldIndex( poFieldDefn->GetNameRef()); if( nSrcIndex < 0 ) { poFeatureDefn->DeleteGeomFieldDefn(i); } else { /* TODO: merge type, SRS, extent ? */ i ++; } } } } return poFeatureDefn; }
OGRMSSQLSpatialSelectLayer::OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *poDSIn, CPLODBCStatement * poStmtIn ) { poDS = poDSIn; iNextShapeId = 0; nSRSId = 0; poFeatureDefn = nullptr; poStmt = poStmtIn; pszBaseStatement = CPLStrdup( poStmtIn->GetCommand() ); /* identify the geometry column */ pszGeomColumn = nullptr; int iImageCol = -1; for ( int iColumn = 0; iColumn < poStmt->GetColCount(); iColumn++ ) { if ( EQUAL(poStmt->GetColTypeName( iColumn ), "image") ) { SQLCHAR szTableName[256]; SQLSMALLINT nTableNameLength = 0; SQLColAttribute(poStmt->GetStatement(), (SQLSMALLINT)(iColumn + 1), SQL_DESC_TABLE_NAME, szTableName, sizeof(szTableName), &nTableNameLength, nullptr); if (nTableNameLength > 0) { OGRLayer *poBaseLayer = poDS->GetLayerByName((const char*)szTableName); if (poBaseLayer != nullptr && EQUAL(poBaseLayer->GetGeometryColumn(), poStmt->GetColName(iColumn))) { nGeomColumnType = MSSQLCOLTYPE_BINARY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); /* copy spatial reference */ if (!poSRS && poBaseLayer->GetSpatialRef()) poSRS = poBaseLayer->GetSpatialRef()->Clone(); break; } } else if (iImageCol == -1) iImageCol = iColumn; } else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geometry") ) { nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); break; } else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geography") ) { nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); break; } else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "udt") ) { SQLCHAR szUDTTypeName[256]; SQLSMALLINT nUDTTypeNameLength = 0; SQLColAttribute(poStmt->GetStatement(), (SQLSMALLINT)(iColumn + 1), SQL_CA_SS_UDT_TYPE_NAME, szUDTTypeName, sizeof(szUDTTypeName), &nUDTTypeNameLength, nullptr); // For some reason on unixODBC, a UCS2 string is returned if ( EQUAL((char*)szUDTTypeName, "geometry") || (nUDTTypeNameLength == 16 && memcmp(szUDTTypeName, "g\0e\0o\0m\0e\0t\0r\0y", 16) == 0) ) { nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); } else if ( EQUAL((char*)szUDTTypeName, "geography") || (nUDTTypeNameLength == 18 && memcmp(szUDTTypeName, "g\0e\0o\0g\0r\0a\0p\0h\0y", 18) == 0) ) { nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn)); } break; } } if (pszGeomColumn == nullptr && iImageCol >= 0) { /* set the image col as geometry column as the last resort */ nGeomColumnType = MSSQLCOLTYPE_BINARY; pszGeomColumn = CPLStrdup(poStmt->GetColName(iImageCol)); } BuildFeatureDefn( "SELECT", poStmt ); if ( GetSpatialRef() && poFeatureDefn->GetGeomFieldCount() == 1) poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef( poSRS ); }
OGRFeature *OGRMSSQLSpatialLayer::GetNextRawFeature() { if( GetStatement() == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* If we are marked to restart then do so, and fetch a record. */ /* -------------------------------------------------------------------- */ if( !poStmt->Fetch() ) { delete poStmt; poStmt = NULL; return NULL; } /* -------------------------------------------------------------------- */ /* Create a feature from the current result. */ /* -------------------------------------------------------------------- */ int iField; OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); if( pszFIDColumn != NULL && poStmt->GetColId(pszFIDColumn) > -1 ) poFeature->SetFID( CPLAtoGIntBig(poStmt->GetColData(poStmt->GetColId(pszFIDColumn))) ); else poFeature->SetFID( iNextShapeId ); iNextShapeId++; m_nFeaturesRead++; /* -------------------------------------------------------------------- */ /* Set the fields. */ /* -------------------------------------------------------------------- */ for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { if ( poFeatureDefn->GetFieldDefn(iField)->IsIgnored() ) continue; int iSrcField = panFieldOrdinals[iField]; const char *pszValue = poStmt->GetColData( iSrcField ); if( pszValue == NULL ) /* no value */; else if( poFeature->GetFieldDefnRef(iField)->GetType() == OFTBinary ) poFeature->SetField( iField, poStmt->GetColDataLength(iSrcField), (GByte *) pszValue ); else poFeature->SetField( iField, pszValue ); } /* -------------------------------------------------------------------- */ /* Try to extract a geometry. */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL && !poFeatureDefn->IsGeometryIgnored()) { int iField = poStmt->GetColId( pszGeomColumn ); const char *pszGeomText = poStmt->GetColData( iField ); OGRGeometry *poGeom = NULL; OGRErr eErr = OGRERR_NONE; if( pszGeomText != NULL ) { int nLength = poStmt->GetColDataLength( iField ); if ( nGeomColumnType == MSSQLCOLTYPE_GEOMETRY || nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY || nGeomColumnType == MSSQLCOLTYPE_BINARY) { switch ( poDS->GetGeometryFormat() ) { case MSSQLGEOMETRY_NATIVE: { OGRMSSQLGeometryParser oParser( nGeomColumnType ); eErr = oParser.ParseSqlGeometry( (unsigned char *) pszGeomText, nLength, &poGeom ); nSRSId = oParser.GetSRSId(); } break; case MSSQLGEOMETRY_WKB: case MSSQLGEOMETRY_WKBZM: eErr = OGRGeometryFactory::createFromWkb((unsigned char *) pszGeomText, NULL, &poGeom, nLength); break; case MSSQLGEOMETRY_WKT: eErr = OGRGeometryFactory::createFromWkt((char **) &pszGeomText, NULL, &poGeom); break; } } else if (nGeomColumnType == MSSQLCOLTYPE_TEXT) { eErr = OGRGeometryFactory::createFromWkt((char **) &pszGeomText, NULL, &poGeom); } } if ( eErr != OGRERR_NONE ) { const char *pszMessage; switch ( eErr ) { case OGRERR_NOT_ENOUGH_DATA: pszMessage = "Not enough data to deserialize"; break; case OGRERR_UNSUPPORTED_GEOMETRY_TYPE: pszMessage = "Unsupported geometry type"; break; case OGRERR_CORRUPT_DATA: pszMessage = "Corrupt data"; break; default: pszMessage = "Unrecognized error"; } CPLError(CE_Failure, CPLE_AppDefined, "GetNextRawFeature(): %s", pszMessage); } if( poGeom != NULL ) { if ( GetSpatialRef() ) poGeom->assignSpatialReference( poSRS ); poFeature->SetGeometryDirectly( poGeom ); } } return poFeature; }
OGRErr OGRMySQLTableLayer::ICreateFeature( OGRFeature *poFeature ) { int bNeedComma = FALSE; CPLString osCommand; /* -------------------------------------------------------------------- */ /* Form the INSERT command. */ /* -------------------------------------------------------------------- */ osCommand.Printf( "INSERT INTO `%s` (", poFeatureDefn->GetName() ); if( poFeature->GetGeometryRef() != nullptr ) { osCommand = osCommand + "`" + pszGeomColumn + "` "; bNeedComma = TRUE; } if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != nullptr ) { if( bNeedComma ) osCommand += ", "; osCommand = osCommand + "`" + pszFIDColumn + "` "; bNeedComma = TRUE; } for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if( !bNeedComma ) bNeedComma = TRUE; else osCommand += ", "; osCommand = osCommand + "`" + poFeatureDefn->GetFieldDefn(i)->GetNameRef() + "`"; } osCommand += ") VALUES ("; // Set the geometry bNeedComma = poFeature->GetGeometryRef() != nullptr; if( poFeature->GetGeometryRef() != nullptr) { char *pszWKT = nullptr; if( poFeature->GetGeometryRef() != nullptr ) { OGRGeometry *poGeom = (OGRGeometry *) poFeature->GetGeometryRef(); poGeom->closeRings(); poGeom->flattenTo2D(); poGeom->exportToWkt( &pszWKT ); } if( pszWKT != nullptr ) { const char* pszAxisOrder = ""; OGRSpatialReference* l_poSRS = GetSpatialRef(); if( poDS->GetMajorVersion() >= 8 && !poDS->IsMariaDB() && l_poSRS && l_poSRS->IsGeographic() ) { pszAxisOrder = ", 'axis-order=long-lat'"; } osCommand += CPLString().Printf( "%s('%s',%d%s) ", poDS->GetMajorVersion() >= 8 ? "ST_GeomFromText" : "GeometryFromText", pszWKT, nSRSId, pszAxisOrder ); CPLFree( pszWKT ); } else osCommand += "''"; } // Set the FID if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != nullptr ) { GIntBig nFID = poFeature->GetFID(); if( !CPL_INT64_FITS_ON_INT32(nFID) && GetMetadataItem(OLMD_FID64) == nullptr ) { CPLString osCommand2; osCommand2.Printf( "ALTER TABLE `%s` MODIFY COLUMN `%s` BIGINT UNIQUE NOT NULL AUTO_INCREMENT", poFeatureDefn->GetName(), pszFIDColumn ); if( mysql_query(poDS->GetConn(), osCommand2 ) ) { poDS->ReportError( osCommand2 ); return OGRERR_FAILURE; } // make sure to attempt to free results of successful queries MYSQL_RES *hResult = mysql_store_result( poDS->GetConn() ); if( hResult != nullptr ) mysql_free_result( hResult ); hResult = nullptr; SetMetadataItem(OLMD_FID64, "YES"); } if( bNeedComma ) osCommand += ", "; osCommand += CPLString().Printf( CPL_FRMT_GIB, nFID ); bNeedComma = TRUE; } for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if( bNeedComma ) osCommand += ", "; else bNeedComma = TRUE; const char *pszStrValue = poFeature->GetFieldAsString(i); if( poFeature->IsFieldNull(i) ) { osCommand += "NULL"; } else if( poFeatureDefn->GetFieldDefn(i)->GetType() != OFTInteger && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTInteger64 && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTReal && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTBinary ) { // We need to quote and escape string fields. osCommand += "'"; for( int iChar = 0; pszStrValue[iChar] != '\0'; iChar++ ) { if( poFeatureDefn->GetFieldDefn(i)->GetType() != OFTIntegerList && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTInteger64List && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTRealList && poFeatureDefn->GetFieldDefn(i)->GetWidth() > 0 && iChar == poFeatureDefn->GetFieldDefn(i)->GetWidth() ) { CPLDebug( "MYSQL", "Truncated %s field value, it was too long.", poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); break; } if( pszStrValue[iChar] == '\\' || pszStrValue[iChar] == '\'' ) { osCommand += '\\'; osCommand += pszStrValue[iChar]; } else osCommand += pszStrValue[iChar]; } osCommand += "'"; } else if( poFeatureDefn->GetFieldDefn(i)->GetType() == OFTBinary ) { int binaryCount = 0; GByte* binaryData = poFeature->GetFieldAsBinary(i, &binaryCount); char* pszHexValue = CPLBinaryToHex( binaryCount, binaryData ); osCommand += "x'"; osCommand += pszHexValue; osCommand += "'"; CPLFree( pszHexValue ); } else { osCommand += pszStrValue; } } osCommand += ")"; //CPLDebug("MYSQL", "%s", osCommand.c_str()); int nQueryResult = mysql_query(poDS->GetConn(), osCommand.c_str() ); const my_ulonglong nFID = mysql_insert_id( poDS->GetConn() ); if( nQueryResult ){ int eErrorCode = mysql_errno(poDS->GetConn()); if (eErrorCode == 1153) {//ER_NET_PACKET_TOO_LARGE) poDS->ReportError("CreateFeature failed because the MySQL server " \ "cannot read the entire query statement. Increase " \ "the size of statements your server will allow by " \ "altering the 'max_allowed_packet' parameter in "\ "your MySQL server configuration."); } else { CPLDebug("MYSQL","Error number %d", eErrorCode); poDS->ReportError( osCommand.c_str() ); } // make sure to attempt to free results MYSQL_RES *hResult = mysql_store_result( poDS->GetConn() ); if( hResult != nullptr ) mysql_free_result( hResult ); hResult = nullptr; return OGRERR_FAILURE; } if( nFID > 0 ) { poFeature->SetFID( nFID ); } // make sure to attempt to free results of successful queries MYSQL_RES *hResult = mysql_store_result( poDS->GetConn() ); if( hResult != nullptr ) mysql_free_result( hResult ); hResult = nullptr; return OGRERR_NONE; }
CPLErr OGRMSSQLSpatialTableLayer::Initialize( const char *pszSchema, const char *pszLayerName, const char *pszGeomCol, int nCoordDimension, int nSRId, OGRwkbGeometryType eType ) { CPLODBCSession *poSession = poDS->GetSession(); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; /* -------------------------------------------------------------------- */ /* Parse out schema name if present in layer. We assume a */ /* schema is provided if there is a dot in the name, and that */ /* it is in the form <schema>.<tablename> */ /* -------------------------------------------------------------------- */ const char *pszDot = strstr(pszLayerName,"."); if( pszDot != NULL ) { pszTableName = CPLStrdup(pszDot + 1); pszSchemaName = CPLStrdup(pszLayerName); pszSchemaName[pszDot - pszLayerName] = '\0'; } else { pszTableName = CPLStrdup(pszLayerName); pszSchemaName = CPLStrdup(pszSchema); } /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName, poDS->GetCatalog(), pszSchemaName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "OGR_MSSQLSpatial", "Table %s has multiple primary key fields, " "ignoring them all.", pszTableName ); } } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName, poDS->GetCatalog(), pszSchemaName ) ) return CE_Failure; eErr = BuildFeatureDefn( pszLayerName, &oGetCol ); if( eErr != CE_None ) return eErr; poFeatureDefn->SetGeomType(eType); if( poFeatureDefn->GetFieldCount() == 0 && pszFIDColumn == NULL && pszGeomColumn == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszLayerName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.GetColId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } else { if ( nGeomColumnType < 0 ) { /* last attempt to identify the geometry column type */ if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geometry") ) nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geography") ) nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "varchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "nvarchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "text") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "ntext") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "image") ) nGeomColumnType = MSSQLCOLTYPE_BINARY; else { CPLError( CE_Failure, CPLE_AppDefined, "Column type %s is not supported for geometry column.", oGetCol.GetColTypeName( iColumn ) ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } } } } /* -------------------------------------------------------------------- */ /* Try to find out the spatial reference */ /* -------------------------------------------------------------------- */ nSRSId = nSRId; if (nSRSId < 0) nSRSId = FetchSRSId(); GetSpatialRef(); return CE_None; }
OGRFeature *OGRDB2Layer::GetNextRawFeature() { if( GetStatement() == nullptr ) return nullptr; /* -------------------------------------------------------------------- */ /* If we are marked to restart then do so, and fetch a record. */ /* -------------------------------------------------------------------- */ if( !m_poStmt->Fetch() ) // fail is normal for final fetch { // CPLDebug("OGR_DB2Layer::GetNextRawFeature","Fetch failed"); delete m_poStmt; m_poStmt = nullptr; return nullptr; } // CPLDebug("OGR_DB2Layer::GetNextRawFeature","Create feature"); /* -------------------------------------------------------------------- */ /* Create a feature from the current result. */ /* -------------------------------------------------------------------- */ int iField; OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); if( pszFIDColumn != nullptr && m_poStmt->GetColId(pszFIDColumn) > -1 ) poFeature->SetFID( atoi(m_poStmt->GetColData(m_poStmt->GetColId(pszFIDColumn))) ); else poFeature->SetFID( iNextShapeId ); iNextShapeId++; m_nFeaturesRead++; /* -------------------------------------------------------------------- */ /* Set the fields. */ /* -------------------------------------------------------------------- */ for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { if ( poFeatureDefn->GetFieldDefn(iField)->IsIgnored() ) continue; int iSrcField = panFieldOrdinals[iField]; const char *pszValue = m_poStmt->GetColData( iSrcField ); if( pszValue == nullptr ) poFeature->SetFieldNull( iField ); else if( poFeature->GetFieldDefnRef(iField)->GetType() == OFTBinary ) poFeature->SetField( iField, m_poStmt->GetColDataLength(iSrcField), (GByte *) pszValue ); else poFeature->SetField( iField, pszValue ); } /* -------------------------------------------------------------------- */ /* Try to extract a geometry. */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != nullptr && !poFeatureDefn->IsGeometryIgnored()) { iField = m_poStmt->GetColId( pszGeomColumn ); const char *pszGeomText = m_poStmt->GetColData( iField ); OGRGeometry *poGeom = nullptr; OGRErr eErr = OGRERR_NONE; if( pszGeomText != nullptr ) { eErr = OGRGeometryFactory::createFromWkt(pszGeomText, nullptr, &poGeom); } if ( eErr != OGRERR_NONE ) { const char *pszMessage; switch ( eErr ) { case OGRERR_NOT_ENOUGH_DATA: pszMessage = "Not enough data to deserialize"; break; case OGRERR_UNSUPPORTED_GEOMETRY_TYPE: pszMessage = "Unsupported geometry type"; break; case OGRERR_CORRUPT_DATA: pszMessage = "Corrupt data"; break; default: pszMessage = "Unrecognized error"; } CPLError(CE_Failure, CPLE_AppDefined, "GetNextRawFeature(): %s", pszMessage); } if( poGeom != nullptr ) { if ( GetSpatialRef() ) poGeom->assignSpatialReference( poSRS ); poFeature->SetGeometryDirectly( poGeom ); } } return poFeature; }