void OGRElasticDataSource::FetchMapping(const char* pszIndexName) { if( m_oSetLayers.find(pszIndexName) != m_oSetLayers.end() ) return; CPLString osURL(m_osURL + CPLString("/") + pszIndexName + CPLString("/_mapping?pretty")); json_object* poRes = RunRequest(osURL, nullptr, std::vector<int>({403})); if( poRes ) { json_object* poLayerObj = CPL_json_object_object_get(poRes, pszIndexName); json_object* poMappings = nullptr; if( poLayerObj && json_object_get_type(poLayerObj) == json_type_object ) poMappings = CPL_json_object_object_get(poLayerObj, "mappings"); if( poMappings && json_object_get_type(poMappings) == json_type_object ) { json_object_iter it; it.key = nullptr; it.val = nullptr; it.entry = nullptr; std::vector<CPLString> aosMappings; json_object_object_foreachC( poMappings, it ) { aosMappings.push_back(it.key); } if( aosMappings.size() == 1 && (aosMappings[0] == "FeatureCollection" || aosMappings[0] == "default") ) { m_oSetLayers.insert(pszIndexName); OGRElasticLayer* poLayer = new OGRElasticLayer( pszIndexName, pszIndexName, aosMappings[0], this, papszOpenOptions); poLayer->InitFeatureDefnFromMapping( CPL_json_object_object_get(poMappings, aosMappings[0]), "", std::vector<CPLString>()); m_apoLayers.push_back(std::unique_ptr<OGRElasticLayer>(poLayer)); } else { for(size_t i=0; i<aosMappings.size();i++) { CPLString osLayerName(pszIndexName + CPLString("_") + aosMappings[i]); if( m_oSetLayers.find(osLayerName) == m_oSetLayers.end() ) { m_oSetLayers.insert(osLayerName); OGRElasticLayer* poLayer = new OGRElasticLayer( osLayerName, pszIndexName, aosMappings[i], this, papszOpenOptions); poLayer->InitFeatureDefnFromMapping( CPL_json_object_object_get(poMappings, aosMappings[i]), "", std::vector<CPLString>()); m_apoLayers.push_back(std::unique_ptr<OGRElasticLayer>(poLayer)); } } } }
int OGRShapeDataSource::Open( GDALOpenInfo* poOpenInfo, int bTestOpen, int bForceSingleFileDataSource ) { CPLAssert( nLayers == 0 ); const char * pszNewName = poOpenInfo->pszFilename; int bUpdate = poOpenInfo->eAccess == GA_Update; papszOpenOptions = CSLDuplicate( poOpenInfo->papszOpenOptions ); pszName = CPLStrdup( pszNewName ); bDSUpdate = bUpdate; bSingleFileDataSource = bForceSingleFileDataSource; /* -------------------------------------------------------------------- */ /* If bSingleFileDataSource is TRUE we don't try to do anything else. */ /* This is only utilized when the OGRShapeDriver::Create() */ /* method wants to create a stub OGRShapeDataSource for a */ /* single shapefile. The driver will take care of creating the */ /* file by callingICreateLayer(). */ /* -------------------------------------------------------------------- */ if( bSingleFileDataSource ) return TRUE; /* -------------------------------------------------------------------- */ /* Is the given path a directory or a regular file? */ /* -------------------------------------------------------------------- */ if( !poOpenInfo->bStatOK ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_AppDefined, "%s is neither a file or directory, Shape access failed.\n", pszNewName ); return FALSE; } /* -------------------------------------------------------------------- */ /* Build a list of filenames we figure are Shape files. */ /* -------------------------------------------------------------------- */ if( !poOpenInfo->bIsDirectory ) { if( !OpenFile( pszNewName, bUpdate, bTestOpen ) ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open shapefile %s.\n" "It may be corrupt or read-only file accessed in update mode.\n", pszNewName ); return FALSE; } bSingleFileDataSource = TRUE; return TRUE; } else { char **papszCandidates = CPLReadDir( pszNewName ); int iCan, nCandidateCount = CSLCount( papszCandidates ); int bMightBeOldCoverage = FALSE; std::set<CPLString> osLayerNameSet; for( iCan = 0; iCan < nCandidateCount; iCan++ ) { char *pszFilename; const char *pszCandidate = papszCandidates[iCan]; const char *pszLayerName = CPLGetBasename(pszCandidate); CPLString osLayerName(pszLayerName); #ifdef WIN32 /* On Windows, as filenames are case insensitive, a shapefile layer can be made of */ /* foo.shp and FOO.DBF, so to detect unique layer names, put them */ /* upper case in the unique set used for detection */ osLayerName.toupper(); #endif if( EQUAL(pszCandidate,"ARC") ) bMightBeOldCoverage = TRUE; if( strlen(pszCandidate) < 4 || !EQUAL(pszCandidate+strlen(pszCandidate)-4,".shp") ) continue; pszFilename = CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, NULL)); osLayerNameSet.insert(osLayerName); #ifdef IMMEDIATE_OPENING if( !OpenFile( pszFilename, bUpdate, bTestOpen ) && !bTestOpen ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open shapefile %s.\n" "It may be corrupt or read-only file accessed in update mode.\n", pszFilename ); CPLFree( pszFilename ); CSLDestroy( papszCandidates ); return FALSE; } #else oVectorLayerName.push_back(pszFilename); #endif CPLFree( pszFilename ); } // Try and .dbf files without apparent associated shapefiles. for( iCan = 0; iCan < nCandidateCount; iCan++ ) { char *pszFilename; const char *pszCandidate = papszCandidates[iCan]; const char *pszLayerName = CPLGetBasename(pszCandidate); CPLString osLayerName(pszLayerName); #ifdef WIN32 osLayerName.toupper(); #endif // We don't consume .dbf files in a directory that looks like // an old style Arc/Info (for PC?) that unless we found at least // some shapefiles. See Bug 493. if( bMightBeOldCoverage && osLayerNameSet.size() == 0 ) continue; if( strlen(pszCandidate) < 4 || !EQUAL(pszCandidate+strlen(pszCandidate)-4,".dbf") ) continue; if( osLayerNameSet.find(osLayerName) != osLayerNameSet.end() ) continue; // We don't want to access .dbf files with an associated .tab // file, or it will never get recognised as a mapinfo dataset. int iCan2, bFoundTAB = FALSE; for( iCan2 = 0; iCan2 < nCandidateCount; iCan2++ ) { const char *pszCandidate2 = papszCandidates[iCan2]; if( EQUALN(pszCandidate2,pszLayerName,strlen(pszLayerName)) && EQUAL(pszCandidate2 + strlen(pszLayerName), ".tab") ) bFoundTAB = TRUE; } if( bFoundTAB ) continue; pszFilename = CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, NULL)); osLayerNameSet.insert(osLayerName); #ifdef IMMEDIATE_OPENING if( !OpenFile( pszFilename, bUpdate, bTestOpen ) && !bTestOpen ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open dbf file %s.\n" "It may be corrupt or read-only file accessed in update mode.\n", pszFilename ); CPLFree( pszFilename ); CSLDestroy( papszCandidates ); return FALSE; } #else oVectorLayerName.push_back(pszFilename); #endif CPLFree( pszFilename ); } CSLDestroy( papszCandidates ); #ifdef IMMEDIATE_OPENING int nDirLayers = nLayers; #else int nDirLayers = oVectorLayerName.size(); #endif CPLErrorReset(); return nDirLayers > 0 || !bTestOpen; } }
OGRErr OGRCSVEditableLayerSynchronizer::EditableSyncToDisk(OGRLayer* poEditableLayer, OGRLayer** ppoDecoratedLayer) { CPLAssert( m_poCSVLayer == *ppoDecoratedLayer ); CPLString osLayerName(m_poCSVLayer->GetName()); CPLString osFilename(m_poCSVLayer->GetFilename()); const bool bCreateCSVT = m_poCSVLayer->GetCreateCSVT(); CPLString osCSVTFilename(CPLResetExtension(osFilename, "csvt")); VSIStatBufL sStatBuf; const bool bHasCSVT = VSIStatL(osCSVTFilename, &sStatBuf) == 0; CPLString osTmpFilename(osFilename); CPLString osTmpCSVTFilename(osFilename); if( VSIStatL(osFilename, &sStatBuf) == 0 ) { osTmpFilename += "_ogr_tmp.csv"; osTmpCSVTFilename += "_ogr_tmp.csvt"; } const char chDelimiter = m_poCSVLayer->GetDelimiter(); OGRCSVLayer* poCSVTmpLayer = new OGRCSVLayer( osLayerName, NULL, osTmpFilename, true, true, chDelimiter ); poCSVTmpLayer->BuildFeatureDefn(NULL, NULL, m_papszOpenOptions); poCSVTmpLayer->SetCRLF( m_poCSVLayer->GetCRLF() ); poCSVTmpLayer->SetCreateCSVT( bCreateCSVT || bHasCSVT ); poCSVTmpLayer->SetWriteBOM( m_poCSVLayer->GetWriteBOM() ); if( m_poCSVLayer->GetGeometryFormat() == OGR_CSV_GEOM_AS_WKT ) poCSVTmpLayer->SetWriteGeometry( wkbNone, OGR_CSV_GEOM_AS_WKT, NULL ); OGRErr eErr = OGRERR_NONE; OGRFeatureDefn* poEditableFDefn = poEditableLayer->GetLayerDefn(); for( int i=0; eErr == OGRERR_NONE && i < poEditableFDefn->GetFieldCount(); i++ ) { OGRFieldDefn oFieldDefn(poEditableFDefn->GetFieldDefn(i)); int iGeomFieldIdx = 0; if( (EQUAL(oFieldDefn.GetNameRef(), "WKT") && (iGeomFieldIdx = poEditableFDefn->GetGeomFieldIndex("")) >= 0) || (iGeomFieldIdx = poEditableFDefn->GetGeomFieldIndex(oFieldDefn.GetNameRef())) >= 0 ) { OGRGeomFieldDefn oGeomFieldDefn( poEditableFDefn->GetGeomFieldDefn(iGeomFieldIdx) ); eErr = poCSVTmpLayer->CreateGeomField( &oGeomFieldDefn ); } else { eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } } const bool bHasXY = ( !m_poCSVLayer->GetXField().empty() && !m_poCSVLayer->GetYField().empty() ); const bool bHasZ = ( !m_poCSVLayer->GetZField().empty() ); if( bHasXY && !CPLFetchBool(m_papszOpenOptions, "KEEP_GEOM_COLUMNS", true) ) { if( poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetXField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetXField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } if( poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetYField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetYField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } if( bHasZ && poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetZField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetZField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } } int nFirstGeomColIdx = 0; if( m_poCSVLayer->HasHiddenWKTColumn() ) { poCSVTmpLayer->SetWriteGeometry( poEditableFDefn->GetGeomFieldDefn(0)->GetType(), OGR_CSV_GEOM_AS_WKT, poEditableFDefn->GetGeomFieldDefn(0)->GetNameRef()); nFirstGeomColIdx = 1; } if( !(poEditableFDefn->GetGeomFieldCount() == 1 && bHasXY) ) { for( int i=nFirstGeomColIdx; eErr == OGRERR_NONE && i < poEditableFDefn->GetGeomFieldCount(); i++ ) { OGRGeomFieldDefn oGeomFieldDefn( poEditableFDefn->GetGeomFieldDefn(i) ); if( poCSVTmpLayer->GetLayerDefn()->GetGeomFieldIndex(oGeomFieldDefn.GetNameRef()) >= 0 ) continue; eErr = poCSVTmpLayer->CreateGeomField( &oGeomFieldDefn ); } } OGRFeature* poFeature = NULL; poEditableLayer->ResetReading(); while( eErr == OGRERR_NONE && (poFeature = poEditableLayer->GetNextFeature()) != NULL ) { OGRFeature* poNewFeature = new OGRFeature( poCSVTmpLayer->GetLayerDefn() ); poNewFeature->SetFrom(poFeature); if( bHasXY ) { OGRGeometry* poGeom = poFeature->GetGeometryRef(); if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { poNewFeature->SetField( m_poCSVLayer->GetXField(), static_cast<OGRPoint*>(poGeom)->getX()); poNewFeature->SetField( m_poCSVLayer->GetYField(), static_cast<OGRPoint*>(poGeom)->getY()); if( bHasZ ) { poNewFeature->SetField( m_poCSVLayer->GetZField(), static_cast<OGRPoint*>(poGeom)->getZ()); } } } eErr = poCSVTmpLayer->CreateFeature(poNewFeature); delete poFeature; delete poNewFeature; } delete poCSVTmpLayer; if( eErr != OGRERR_NONE ) { CPLError(CE_Failure, CPLE_AppDefined, "Error while creating %s", osTmpFilename.c_str()); VSIUnlink( osTmpFilename ); VSIUnlink( CPLResetExtension(osTmpFilename, "csvt") ); return eErr; } delete m_poCSVLayer; if( osFilename != osTmpFilename ) { CPLString osTmpOriFilename(osFilename + ".ogr_bak"); CPLString osTmpOriCSVTFilename(osCSVTFilename + ".ogr_bak"); if( VSIRename( osFilename, osTmpOriFilename ) != 0 || (bHasCSVT && VSIRename( osCSVTFilename, osTmpOriCSVTFilename ) != 0 ) || VSIRename( osTmpFilename, osFilename) != 0 || (bHasCSVT && VSIRename( osTmpCSVTFilename, osCSVTFilename ) != 0) ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename files"); *ppoDecoratedLayer = NULL; m_poCSVLayer = NULL; return OGRERR_FAILURE; } VSIUnlink( osTmpOriFilename ); if( bHasCSVT ) VSIUnlink( osTmpOriCSVTFilename ); } VSILFILE* fp = VSIFOpenL( osFilename, "rb+" ); if( fp == NULL ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot reopen updated %s", osFilename.c_str()); *ppoDecoratedLayer = NULL; m_poCSVLayer = NULL; return OGRERR_FAILURE; } m_poCSVLayer = new OGRCSVLayer( osLayerName, fp, osFilename, false, /* new */ true, /* update */ chDelimiter ); m_poCSVLayer->BuildFeatureDefn(NULL, NULL, m_papszOpenOptions); *ppoDecoratedLayer = m_poCSVLayer; return OGRERR_NONE; }
int OGRGFTDataSource::Open( const char * pszFilename, int bUpdateIn) { if (!EQUALN(pszFilename, "GFT:", 4)) return FALSE; bReadWrite = bUpdateIn; pszName = CPLStrdup( pszFilename ); osAuth = OGRGFTGetOptionValue(pszFilename, "auth"); if (osAuth.size() == 0) osAuth = CPLGetConfigOption("GFT_AUTH", ""); osRefreshToken = OGRGFTGetOptionValue(pszFilename, "refresh"); if (osRefreshToken.size() == 0) osRefreshToken = CPLGetConfigOption("GFT_REFRESH_TOKEN", ""); osAPIKey = CPLGetConfigOption("GFT_APIKEY", GDAL_API_KEY); CPLString osTables = OGRGFTGetOptionValue(pszFilename, "tables"); bUseHTTPS = TRUE; osAccessToken = OGRGFTGetOptionValue(pszFilename, "access"); if (osAccessToken.size() == 0) osAccessToken = CPLGetConfigOption("GFT_ACCESS_TOKEN",""); if (osAccessToken.size() == 0 && osRefreshToken.size() > 0) { osAccessToken.Seize(GOA2GetAccessToken(osRefreshToken, FUSION_TABLE_SCOPE)); if (osAccessToken.size() == 0) return FALSE; } if (osAccessToken.size() == 0 && osAuth.size() > 0) { osRefreshToken.Seize(GOA2GetRefreshToken(osAuth, FUSION_TABLE_SCOPE)); if (osRefreshToken.size() == 0) return FALSE; } if (osAccessToken.size() == 0) { if (osTables.size() == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Unauthenticated access requires explicit tables= parameter"); return FALSE; } } if (osTables.size() != 0) { char** papszTables = CSLTokenizeString2(osTables, ",", 0); for(int i=0;papszTables && papszTables[i];i++) { papoLayers = (OGRLayer**) CPLRealloc(papoLayers, (nLayers + 1) * sizeof(OGRLayer*)); papoLayers[nLayers ++] = new OGRGFTTableLayer(this, papszTables[i], papszTables[i]); } CSLDestroy(papszTables); return TRUE; } /* Get list of tables */ CPLHTTPResult * psResult = RunSQL("SHOW TABLES"); if (psResult == NULL) return FALSE; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL || strncmp(pszLine, "table id,name", strlen("table id,name")) != 0) { CPLHTTPDestroyResult(psResult); return FALSE; } pszLine = OGRGFTGotoNextLine(pszLine); while(pszLine != NULL && *pszLine != 0) { char* pszNextLine = OGRGFTGotoNextLine(pszLine); if (pszNextLine) pszNextLine[-1] = 0; char** papszTokens = CSLTokenizeString2(pszLine, ",", 0); if (CSLCount(papszTokens) == 2) { CPLString osTableId(papszTokens[0]); CPLString osLayerName(papszTokens[1]); for(int i=0;i<nLayers;i++) { if (strcmp(papoLayers[i]->GetName(), osLayerName) == 0) { osLayerName += " ("; osLayerName += osTableId; osLayerName += ")"; break; } } papoLayers = (OGRLayer**) CPLRealloc(papoLayers, (nLayers + 1) * sizeof(OGRLayer*)); papoLayers[nLayers ++] = new OGRGFTTableLayer(this, osLayerName, osTableId); } CSLDestroy(papszTokens); pszLine = pszNextLine; } CPLHTTPDestroyResult(psResult); return TRUE; }
bool OGRGeoJSONSeqDataSource::Open( GDALOpenInfo* poOpenInfo, GeoJSONSourceType nSrcType) { VSILFILE* fp = nullptr; CPLString osLayerName("GeoJSONSeq"); const char* pszUnprefixedFilename = poOpenInfo->pszFilename; if (STARTS_WITH_CI(poOpenInfo->pszFilename, "GeoJSONSeq:") ) { pszUnprefixedFilename = poOpenInfo->pszFilename + strlen("GeoJSONSeq:"); } if( nSrcType == eGeoJSONSourceFile ) { if (pszUnprefixedFilename != poOpenInfo->pszFilename) { osLayerName = CPLGetBasename(pszUnprefixedFilename); fp = VSIFOpenL( pszUnprefixedFilename, "rb"); } else { osLayerName = CPLGetBasename(poOpenInfo->pszFilename); fp = poOpenInfo->fpL; poOpenInfo->fpL = nullptr; } } else if( nSrcType == eGeoJSONSourceText ) { m_osTmpFile = CPLSPrintf("/vsimem/geojsonseq/%p", this); fp = VSIFileFromMemBuffer( m_osTmpFile.c_str(), reinterpret_cast<GByte*>(CPLStrdup(poOpenInfo->pszFilename)), strlen(poOpenInfo->pszFilename), true ); } else if( nSrcType == eGeoJSONSourceService ) { char* pszStoredContent = OGRGeoJSONDriverStealStoredContent(pszUnprefixedFilename); if( pszStoredContent ) { if( !GeoJSONSeqIsObject( pszStoredContent) ) { OGRGeoJSONDriverStoreContent( poOpenInfo->pszFilename, pszStoredContent ); return false; } else { m_osTmpFile = CPLSPrintf("/vsimem/geojsonseq/%p", this); fp = VSIFileFromMemBuffer( m_osTmpFile.c_str(), reinterpret_cast<GByte*>(pszStoredContent), strlen(pszStoredContent), true ); } } else { const char* const papsOptions[] = { "HEADERS=Accept: text/plain, application/json", nullptr }; CPLHTTPResult* pResult = CPLHTTPFetch( pszUnprefixedFilename, papsOptions ); if( nullptr == pResult || 0 == pResult->nDataLen || 0 != CPLGetLastErrorNo() ) { CPLHTTPDestroyResult( pResult ); return false; } if( 0 != pResult->nStatus ) { CPLError( CE_Failure, CPLE_AppDefined, "Curl reports error: %d: %s", pResult->nStatus, pResult->pszErrBuf ); CPLHTTPDestroyResult( pResult ); return false; } m_osTmpFile = CPLSPrintf("/vsimem/geojsonseq/%p", this); fp = VSIFileFromMemBuffer( m_osTmpFile.c_str(), pResult->pabyData, pResult->nDataLen, true ); pResult->pabyData = nullptr; pResult->nDataLen = 0; CPLHTTPDestroyResult( pResult ); } } if( fp == nullptr ) { return false; } SetDescription( poOpenInfo->pszFilename ); m_poLayer.reset(new OGRGeoJSONSeqLayer(this, osLayerName.c_str(), fp)); return true; }