OGRErr OGRGFTTableLayer::CommitTransaction() { GetLayerDefn(); if (!bInTransaction) { CPLError(CE_Failure, CPLE_AppDefined, "Should be in transaction"); return OGRERR_FAILURE; } bInTransaction = FALSE; if (nFeaturesInTransaction > 0) { if (nFeaturesInTransaction > 1) osTransaction += ";"; CPLHTTPResult * psResult = poDS->RunSQL(osTransaction); osTransaction.resize(0); nFeaturesInTransaction = 0; if (psResult == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "CommitTransaction failed"); return OGRERR_FAILURE; } char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "rowid", 5) != 0 || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "CommitTransaction failed : %s", pszLine ? pszLine : psResult->pszErrBuf); CPLHTTPDestroyResult(psResult); return OGRERR_FAILURE; } pszLine = OGRGFTGotoNextLine(pszLine); while(pszLine && *pszLine != 0) { char* pszNextLine = OGRGFTGotoNextLine(pszLine); if (pszNextLine) pszNextLine[-1] = 0; //CPLDebug("GFT", "Feature id = %s", pszLine); pszLine = pszNextLine; } CPLHTTPDestroyResult(psResult); } return OGRERR_NONE; }
OGRFeature * OGRGFTTableLayer::GetFeature( long nFID ) { GetLayerDefn(); CPLString osSQL("SELECT ROWID"); for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { osSQL += ","; const char* pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); osSQL += EscapeAndQuote(pszFieldName); } if (bHiddenGeometryField) { osSQL += ","; osSQL += EscapeAndQuote(GetGeometryColumn()); } osSQL += " FROM "; osSQL += osTableId; osSQL += CPLSPrintf(" WHERE ROWID='%ld'", nFID); CPLPushErrorHandler(CPLQuietErrorHandler); CPLHTTPResult * psResult = poDS->RunSQL(osSQL); CPLPopErrorHandler(); if (psResult == NULL) return NULL; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL) { CPLHTTPDestroyResult(psResult); return NULL; } /* skip header line */ pszLine = OGRGFTGotoNextLine(pszLine); if (pszLine == NULL || pszLine[0] == 0) { CPLHTTPDestroyResult(psResult); return NULL; } int nLen = (int)strlen(pszLine); if (nLen > 0 && pszLine[nLen-1] == '\n') pszLine[nLen-1] = '\0'; OGRFeature* poFeature = BuildFeatureFromSQL(pszLine); CPLHTTPDestroyResult(psResult); return poFeature; }
int OGRGFTResultLayer::FetchNextRows() { if (!EQUALN(osSQL.c_str(), "SELECT", 6)) return FALSE; aosRows.resize(0); CPLString osChangedSQL(osSQL); if (osSQL.ifind(" OFFSET ") == std::string::npos && osSQL.ifind(" LIMIT ") == std::string::npos) { osChangedSQL += CPLSPrintf(" OFFSET %d LIMIT %d", nOffset, GetFeaturesToFetch()); } CPLPushErrorHandler(CPLQuietErrorHandler); CPLHTTPResult * psResult = poDS->RunSQL(osChangedSQL); CPLPopErrorHandler(); if (psResult == NULL) { bEOF = TRUE; return FALSE; } char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "RunSQL() failed"); CPLHTTPDestroyResult(psResult); bEOF = TRUE; return FALSE; } pszLine = OGRGFTGotoNextLine(pszLine); if (pszLine == NULL) { CPLHTTPDestroyResult(psResult); bEOF = TRUE; return FALSE; } ParseCSVResponse(pszLine, aosRows); CPLHTTPDestroyResult(psResult); bEOF = (int)aosRows.size() < GetFeaturesToFetch(); return TRUE; }
GIntBig OGRESRIFeatureServiceLayer::GetFeatureCount( int bForce ) { GIntBig nFeatureCount = -1; if( m_poAttrQuery == NULL && m_poFilterGeom == NULL ) { CPLString osNewURL = CPLURLAddKVP(poDS->GetURL(), "returnCountOnly", "true"); CPLHTTPResult* pResult = NULL; CPLErrorReset(); pResult = CPLHTTPFetch( osNewURL, NULL ); if( pResult != NULL && pResult->nDataLen != 0 && CPLGetLastErrorNo() == 0 && pResult->nStatus == 0 ) { const char* pszCount = strstr((const char*)pResult->pabyData, "\"count\""); if( pszCount ) { pszCount = strchr(pszCount, ':'); if( pszCount ) { pszCount++; nFeatureCount = CPLAtoGIntBig(pszCount); } } } CPLHTTPDestroyResult( pResult ); } if( nFeatureCount < 0 ) nFeatureCount = OGRLayer::GetFeatureCount(bForce); return nFeatureCount; }
OGRErr OGRESRIFeatureServiceLayer::GetExtent(OGREnvelope *psExtent, int bForce) { OGRErr eErr = OGRERR_FAILURE; CPLString osNewURL = CPLURLAddKVP(poDS->GetURL(), "returnExtentOnly", "true"); osNewURL = CPLURLAddKVP(osNewURL, "f", "geojson"); CPLErrorReset(); CPLHTTPResult* pResult = CPLHTTPFetch( osNewURL, NULL ); if( pResult != NULL && pResult->nDataLen != 0 && CPLGetLastErrorNo() == 0 && pResult->nStatus == 0 ) { const char* pszBBox = strstr((const char*)pResult->pabyData, "\"bbox\""); if( pszBBox ) { pszBBox = strstr(pszBBox, ":["); if( pszBBox ) { pszBBox+=2; char** papszTokens = CSLTokenizeString2(pszBBox, ",", 0); if( CSLCount(papszTokens) >= 4 ) { psExtent->MinX = CPLAtof(papszTokens[0]); psExtent->MinY = CPLAtof(papszTokens[1]); psExtent->MaxX = CPLAtof(papszTokens[2]); psExtent->MaxY = CPLAtof(papszTokens[3]); eErr = OGRERR_NONE; } CSLDestroy(papszTokens); } } } CPLHTTPDestroyResult( pResult ); if( eErr == OGRERR_FAILURE ) eErr = OGRLayer::GetExtent(psExtent, bForce); return eErr; }
bool OGRAmigoCloudDataSource::RunDELETE(const char*pszURL) { CPLString osURL(pszURL); /* -------------------------------------------------------------------- */ /* Provide the API Key */ /* -------------------------------------------------------------------- */ if( !osAPIKey.empty() ) { if(osURL.find("?") == std::string::npos) osURL += "?token="; else osURL += "&token="; osURL += osAPIKey; } char** papszOptions=nullptr; CPLString osPOSTFIELDS("CUSTOMREQUEST=DELETE"); papszOptions = CSLAddString(papszOptions, osPOSTFIELDS); papszOptions = CSLAddString(papszOptions, GetUserAgentOption().c_str()); CPLHTTPResult * psResult = CPLHTTPFetch( osURL.c_str(), papszOptions); CSLDestroy(papszOptions); if( psResult == nullptr ) return false; if (psResult->pszContentType && strncmp(psResult->pszContentType, "text/html", 9) == 0) { CPLDebug( "AMIGOCLOUD", "RunDELETE HTML Response:%s", psResult->pabyData ); CPLError(CE_Failure, CPLE_AppDefined, "HTML error page returned by server:%s", psResult->pabyData); CPLHTTPDestroyResult(psResult); return false; } if (psResult->pszErrBuf != nullptr && psResult->pabyData != nullptr ) { CPLError( CE_Failure, CPLE_AppDefined, "DELETE Response: %s", psResult->pabyData ); } else if ( psResult->nStatus != 0) { CPLDebug( "AMIGOCLOUD", "DELETE Error Status:%d", psResult->nStatus ); } CPLHTTPDestroyResult(psResult); return true; }
int OGRElasticDataSource::GetLayerCount() { if( m_bAllLayersListed ) { return static_cast<int>(m_apoLayers.size()); } m_bAllLayersListed = true; CPLHTTPResult* psResult = HTTPFetch((m_osURL + "/_cat/indices?h=i").c_str(), nullptr); if( psResult == nullptr || psResult->pszErrBuf != nullptr || psResult->pabyData == nullptr ) { CPLHTTPDestroyResult(psResult); return 0; } char* pszCur = (char*)psResult->pabyData; char* pszNextEOL = strchr(pszCur, '\n'); while( pszNextEOL && pszNextEOL > pszCur ) { *pszNextEOL = '\0'; char* pszBeforeEOL = pszNextEOL - 1; while( *pszBeforeEOL == ' ' ) { *pszBeforeEOL = '\0'; pszBeforeEOL --; } const char* pszIndexName = pszCur; pszCur = pszNextEOL + 1; pszNextEOL = strchr(pszCur, '\n'); if( STARTS_WITH(pszIndexName, ".security") || STARTS_WITH(pszIndexName, ".monitoring") ) { continue; } FetchMapping(pszIndexName); } CPLHTTPDestroyResult(psResult); return static_cast<int>(m_apoLayers.size()); }
GIntBig OGRGFTTableLayer::GetFeatureCount(CPL_UNUSED int bForce) { GetLayerDefn(); CPLString osSQL("SELECT COUNT() FROM "); osSQL += osTableId; if (osWHERE.size()) { osSQL += " "; osSQL += osWHERE; } CPLHTTPResult * psResult = poDS->RunSQL(osSQL); if (psResult == NULL) return 0; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "count()", 7) != 0 || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "GetFeatureCount() failed"); CPLHTTPDestroyResult(psResult); return 0; } pszLine = OGRGFTGotoNextLine(pszLine); if (pszLine == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "GetFeatureCount() failed"); CPLHTTPDestroyResult(psResult); return 0; } char* pszNextLine = OGRGFTGotoNextLine(pszLine); if (pszNextLine) pszNextLine[-1] = 0; int nFeatureCount = atoi(pszLine); CPLHTTPDestroyResult(psResult); return nFeatureCount; }
void OGRElasticDataSource::DeleteIndex(const CPLString &url) { char** papszOptions = NULL; papszOptions = CSLAddNameValue(papszOptions, "CUSTOMREQUEST", "DELETE"); CPLHTTPResult* psResult = CPLHTTPFetch(url, papszOptions); CSLDestroy(papszOptions); if (psResult) { CPLHTTPDestroyResult(psResult); } }
CPLHTTPResult * OGRGFTDataSource::RunSQL(const char* pszUnescapedSQL) { CPLString osSQL("POSTFIELDS=sql="); /* Do post escaping */ for(int i=0;pszUnescapedSQL[i] != 0;i++) { const int ch = ((unsigned char*)pszUnescapedSQL)[i]; if (ch != '&' && ch >= 32 && ch < 128) osSQL += (char)ch; else osSQL += CPLSPrintf("%%%02X", ch); } /* -------------------------------------------------------------------- */ /* Provide the API Key - used to rate limit access (see */ /* GFT_APIKEY config) */ /* -------------------------------------------------------------------- */ osSQL += "&key="; osSQL += osAPIKey; /* -------------------------------------------------------------------- */ /* Force old style CSV output from calls - maybe we want to */ /* migrate to JSON output at some point? */ /* -------------------------------------------------------------------- */ osSQL += "&alt=csv"; /* -------------------------------------------------------------------- */ /* Collection the header options and execute request. */ /* -------------------------------------------------------------------- */ char** papszOptions = CSLAddString(AddHTTPOptions(), osSQL); CPLHTTPResult * psResult = CPLHTTPFetch( GetAPIURL(), papszOptions); CSLDestroy(papszOptions); /* -------------------------------------------------------------------- */ /* Check for some error conditions and report. HTML Messages */ /* are transformed info failure. */ /* -------------------------------------------------------------------- */ if (psResult && psResult->pszContentType && strncmp(psResult->pszContentType, "text/html", 9) == 0) { CPLDebug( "GFT", "RunSQL HTML Response:%s", psResult->pabyData ); CPLError(CE_Failure, CPLE_AppDefined, "HTML error page returned by server"); CPLHTTPDestroyResult(psResult); psResult = NULL; } if (psResult && psResult->pszErrBuf != NULL) { CPLDebug( "GFT", "RunSQL Error Message:%s", psResult->pszErrBuf ); } else if (psResult && psResult->nStatus != 0) { CPLDebug( "GFT", "RunSQL Error Status:%d", psResult->nStatus ); } return psResult; }
void OGRElasticDataSource::UploadFile(const CPLString &url, const CPLString &data) { char** papszOptions = NULL; papszOptions = CSLAddNameValue(papszOptions, "POSTFIELDS", data.c_str()); papszOptions = CSLAddNameValue(papszOptions, "HEADERS", "Content-Type: application/x-javascript; charset=UTF-8"); CPLHTTPResult* psResult = CPLHTTPFetch(url, papszOptions); CSLDestroy(papszOptions); if (psResult) { CPLHTTPDestroyResult(psResult); } }
int OGRElasticDataSource::Create(const char *pszFilename, CPL_UNUSED char **papszOptions) { this->pszName = CPLStrdup(pszFilename); const char* pszMetaFile = CPLGetConfigOption("ES_META", NULL); const char* pszWriteMap = CPLGetConfigOption("ES_WRITEMAP", NULL);; this->bOverwrite = CSLTestBoolean(CPLGetConfigOption("ES_OVERWRITE", "0")); this->nBulkUpload = (int) CPLAtof(CPLGetConfigOption("ES_BULK", "0")); if (pszWriteMap != NULL) { this->pszWriteMap = CPLStrdup(pszWriteMap); } // Read in the meta file from disk if (pszMetaFile != NULL) { int fsize; char *fdata; FILE *fp; fp = fopen(pszMetaFile, "rb"); if (fp != NULL) { fseek(fp, 0, SEEK_END); fsize = (int) ftell(fp); fdata = (char *) malloc(fsize + 1); fseek(fp, 0, SEEK_SET); if (0 == fread(fdata, fsize, 1, fp)) { CPLError(CE_Failure, CPLE_FileIO, "OGRElasticDataSource::Create read failed."); } fdata[fsize] = 0; this->pszMapping = fdata; fclose(fp); } } // Do a status check to ensure that the server is valid CPLHTTPResult* psResult = CPLHTTPFetch(CPLSPrintf("%s/_status", pszFilename), NULL); int bOK = (psResult != NULL && psResult->pszErrBuf == NULL); if (!bOK) { CPLError(CE_Failure, CPLE_NoWriteAccess, "Could not connect to server"); } CPLHTTPDestroyResult(psResult); return bOK; }
OGRPLScenesDataV1Dataset::~OGRPLScenesDataV1Dataset() { for( int i = 0; i < m_nLayers; i++ ) delete m_papoLayers[i]; CPLFree(m_papoLayers); if( m_bMustCleanPersistent ) { char **papszOptions = CSLSetNameValue( nullptr, "CLOSE_PERSISTENT", CPLSPrintf("PLSCENES:%p", this)); CPLHTTPDestroyResult(CPLHTTPFetch(m_osBaseURL, papszOptions)); CSLDestroy(papszOptions); } }
OGRCouchDBDataSource::~OGRCouchDBDataSource() { for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers ); if( bMustCleanPersistent ) { char** papszOptions = NULL; papszOptions = CSLSetNameValue(papszOptions, "CLOSE_PERSISTENT", CPLSPrintf("CouchDB:%p", this)); CPLHTTPDestroyResult( CPLHTTPFetch( osURL, papszOptions ) ); CSLDestroy(papszOptions); } CPLFree( pszName ); }
char* OGRCouchDBDataSource::GetETag(const char* pszURI) { // make a head request and only return the etag response header char* pszEtag = NULL; char **papszTokens; char** papszOptions = NULL; bMustCleanPersistant = TRUE; papszOptions = CSLAddString(papszOptions, CPLSPrintf("PERSISTENT=CouchDB:%p", this)); papszOptions = CSLAddString(papszOptions, "HEADERS=Content-Type: application/json"); papszOptions = CSLAddString(papszOptions, "NO_BODY=1"); if (osUserPwd.size()) { CPLString osUserPwdOption("USERPWD="); osUserPwdOption += osUserPwd; papszOptions = CSLAddString(papszOptions, osUserPwdOption); } CPLDebug("CouchDB", "HEAD %s", pszURI); CPLString osFullURL(osURL); osFullURL += pszURI; CPLPushErrorHandler(CPLQuietErrorHandler); CPLHTTPResult * psResult = CPLHTTPFetch( osFullURL, papszOptions); CPLPopErrorHandler(); CSLDestroy(papszOptions); if (psResult == NULL) return NULL; if (CSLFetchNameValue(psResult->papszHeaders, "Etag") != NULL) { papszTokens = CSLTokenizeString2( CSLFetchNameValue(psResult->papszHeaders, "Etag"), "\"\r\n", 0 ); pszEtag = CPLStrdup(papszTokens[0]); CSLDestroy( papszTokens ); } CPLHTTPDestroyResult(psResult); return pszEtag; }
OGRGFTDataSource::~OGRGFTDataSource() { for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers ); if (bMustCleanPersistent) { char** papszOptions = nullptr; papszOptions = CSLSetNameValue(papszOptions, "CLOSE_PERSISTENT", CPLSPrintf("GFT:%p", this)); CPLHTTPDestroyResult( CPLHTTPFetch( GetAPIURL(), papszOptions) ); CSLDestroy(papszOptions); } CPLFree( pszName ); }
OGRAmigoCloudDataSource::~OGRAmigoCloudDataSource() { for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers ); if( bMustCleanPersistent ) { char** papszOptions = NULL; papszOptions = CSLSetNameValue(papszOptions, "CLOSE_PERSISTENT", CPLSPrintf("AMIGOCLOUD:%p", this)); CPLHTTPDestroyResult( CPLHTTPFetch( GetAPIURL(), papszOptions) ); CSLDestroy(papszOptions); } CPLFree( pszName ); CPLFree(pszProjetctId); }
bool CPLJSONDocument::LoadUrl(const std::string & /*osUrl*/, char ** /*papszOptions*/, GDALProgressFunc /*pfnProgress*/, void * /*pProgressArg*/) #endif // HAVE_CURL { #ifdef HAVE_CURL int nDepth = atoi( CSLFetchNameValueDef( papszOptions, "JSON_DEPTH", "10") ); JsonContext ctx = { nullptr, json_tokener_new_ex(nDepth), 0 }; CPLHTTPFetchWriteFunc pWriteFunc = CPLJSONWriteFunction; CPLHTTPResult *psResult = CPLHTTPFetchEx( osUrl.c_str(), papszOptions, pfnProgress, pProgressArg, pWriteFunc, &ctx ); bool bResult = true; if( psResult->nStatus != 0 /*CURLE_OK*/ ) { bResult = false; } CPLHTTPDestroyResult( psResult ); enum json_tokener_error jerr; if ((jerr = json_tokener_get_error(ctx.pTokener)) != json_tokener_success) { CPLError(CE_Failure, CPLE_AppDefined, "JSON error: %s\n", json_tokener_error_desc(jerr)); bResult = false; } else { if( m_poRootJsonObject ) json_object_put( TO_JSONOBJ(m_poRootJsonObject) ); m_poRootJsonObject = ctx.pObject; } json_tokener_free(ctx.pTokener); return bResult; #else return false; #endif }
static CPLXMLNode* GMLParseXMLFile(const char* pszFilename) { if( STARTS_WITH(pszFilename, "http://") || STARTS_WITH(pszFilename, "https://") ) { CPLXMLNode* psRet = NULL; CPLHTTPResult* psResult = CPLHTTPFetch( pszFilename, NULL ); if( psResult != NULL ) { if( psResult->pabyData != NULL ) { psRet = CPLParseXMLString( (const char*) psResult->pabyData ); } CPLHTTPDestroyResult(psResult); } return psRet; } else { return CPLParseXMLFile(pszFilename); } }
json_object* OGRCouchDBDataSource::REQUEST(const char* pszVerb, const char* pszURI, const char* pszData) { bMustCleanPersistent = true; char** papszOptions = NULL; papszOptions = CSLAddString(papszOptions, CPLSPrintf("PERSISTENT=CouchDB:%p", this)); CPLString osCustomRequest("CUSTOMREQUEST="); osCustomRequest += pszVerb; papszOptions = CSLAddString(papszOptions, osCustomRequest); CPLString osPOSTFIELDS("POSTFIELDS="); if (pszData) osPOSTFIELDS += pszData; papszOptions = CSLAddString(papszOptions, osPOSTFIELDS); papszOptions = CSLAddString(papszOptions, "HEADERS=Content-Type: application/json"); if (!osUserPwd.empty() ) { CPLString osUserPwdOption("USERPWD="); osUserPwdOption += osUserPwd; papszOptions = CSLAddString(papszOptions, osUserPwdOption); } CPLDebug("CouchDB", "%s %s", pszVerb, pszURI); CPLString osFullURL(osURL); osFullURL += pszURI; CPLPushErrorHandler(CPLQuietErrorHandler); CPLHTTPResult * psResult = CPLHTTPFetch( osFullURL, papszOptions); CPLPopErrorHandler(); CSLDestroy(papszOptions); if (psResult == NULL) return NULL; const char* pszServer = CSLFetchNameValue(psResult->papszHeaders, "Server"); if (pszServer == NULL || !STARTS_WITH_CI(pszServer, "CouchDB")) { CPLHTTPDestroyResult(psResult); return NULL; } if (psResult->nDataLen == 0) { CPLHTTPDestroyResult(psResult); return NULL; } json_object* jsobj = NULL; const char* pszText = reinterpret_cast<const char*>(psResult->pabyData); if( !OGRJSonParse(pszText, &jsobj, true) ) { CPLHTTPDestroyResult(psResult); return NULL; } CPLHTTPDestroyResult(psResult); return jsobj; }
json_object* OGRAmigoCloudDataSource::RunSQL(const char* pszUnescapedSQL) { CPLString osSQL; osSQL = "/users/0/projects/" + CPLString(pszProjectId) + "/sql"; /* -------------------------------------------------------------------- */ /* Provide the API Key */ /* -------------------------------------------------------------------- */ if( !osAPIKey.empty() ) { osSQL += "?token=" + osAPIKey; } osSQL += "&query="; char * pszEscaped = CPLEscapeString( pszUnescapedSQL, -1, CPLES_URL ); std::string escaped = pszEscaped; CPLFree( pszEscaped ); osSQL += escaped; /* -------------------------------------------------------------------- */ /* Collection the header options and execute request. */ /* -------------------------------------------------------------------- */ std::string pszAPIURL = GetAPIURL(); char** papszOptions = nullptr; papszOptions = CSLAddString(papszOptions, GetUserAgentOption().c_str()); pszAPIURL += osSQL; CPLHTTPResult * psResult = CPLHTTPFetch( pszAPIURL.c_str(), papszOptions); CSLDestroy(papszOptions); if( psResult == nullptr ) return nullptr; /* -------------------------------------------------------------------- */ /* Check for some error conditions and report. HTML Messages */ /* are transformed info failure. */ /* -------------------------------------------------------------------- */ if (psResult->pszContentType && strncmp(psResult->pszContentType, "text/html", 9) == 0) { CPLDebug( "AMIGOCLOUD", "RunSQL HTML Response:%s", psResult->pabyData ); CPLError(CE_Failure, CPLE_AppDefined, "HTML error page returned by server"); CPLHTTPDestroyResult(psResult); return nullptr; } if (psResult->pszErrBuf != nullptr && psResult->pabyData != nullptr ) { CPLError( CE_Failure, CPLE_AppDefined, "GET Response: %s", psResult->pabyData ); } else if (psResult->nStatus != 0) { CPLDebug( "AMIGOCLOUD", "RunGET Error Status:%d", psResult->nStatus ); } if( psResult->pabyData == nullptr ) { CPLHTTPDestroyResult(psResult); return nullptr; } CPLDebug( "AMIGOCLOUD", "RunSQL Response:%s", psResult->pabyData ); json_object* poObj = nullptr; const char* pszText = reinterpret_cast<const char*>(psResult->pabyData); if( !OGRJSonParse(pszText, &poObj, true) ) { CPLHTTPDestroyResult(psResult); return nullptr; } CPLHTTPDestroyResult(psResult); if( poObj != nullptr ) { if( json_object_get_type(poObj) == json_type_object ) { json_object* poError = CPL_json_object_object_get(poObj, "error"); if( poError != nullptr && json_object_get_type(poError) == json_type_array && json_object_array_length(poError) > 0 ) { poError = json_object_array_get_idx(poError, 0); if( poError != nullptr && json_object_get_type(poError) == json_type_string ) { CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s", json_object_get_string(poError)); json_object_put(poObj); return nullptr; } } } else { json_object_put(poObj); return nullptr; } } return poObj; }
json_object* OGRAmigoCloudDataSource::RunGET(const char*pszURL) { CPLString osURL(pszURL); /* -------------------------------------------------------------------- */ /* Provide the API Key */ /* -------------------------------------------------------------------- */ if( osAPIKey.size() > 0 ) { osURL += "?token="; osURL += osAPIKey; } CPLHTTPResult * psResult = CPLHTTPFetch( osURL.c_str(), NULL); if( psResult == NULL ) return NULL; if (psResult->pszContentType && strncmp(psResult->pszContentType, "text/html", 9) == 0) { CPLDebug( "AMIGOCLOUD", "RunGET HTML Response:%s", psResult->pabyData ); CPLError(CE_Failure, CPLE_AppDefined, "HTML error page returned by server:%s", psResult->pabyData); CPLHTTPDestroyResult(psResult); return NULL; } if ( psResult->pszErrBuf != NULL) { CPLDebug( "AMIGOCLOUD", "RunGET Error Message:%s", psResult->pszErrBuf ); } else if (psResult->nStatus != 0) { CPLDebug( "AMIGOCLOUD", "RunGET Error Status:%d", psResult->nStatus ); } if( psResult->pabyData == NULL ) { CPLHTTPDestroyResult(psResult); return NULL; } CPLDebug( "AMIGOCLOUD", "RunGET Response:%s", psResult->pabyData ); json_tokener* jstok = NULL; json_object* poObj = NULL; jstok = json_tokener_new(); poObj = json_tokener_parse_ex(jstok, (const char*) psResult->pabyData, -1); if( jstok->err != json_tokener_success) { CPLError( CE_Failure, CPLE_AppDefined, "JSON parsing error: %s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset); json_tokener_free(jstok); CPLHTTPDestroyResult(psResult); return NULL; } json_tokener_free(jstok); CPLHTTPDestroyResult(psResult); if( poObj != NULL ) { if( json_object_get_type(poObj) == json_type_object ) { json_object* poError = CPL_json_object_object_get(poObj, "error"); if( poError != NULL && json_object_get_type(poError) == json_type_array && json_object_array_length(poError) > 0 ) { poError = json_object_array_get_idx(poError, 0); if( poError != NULL && json_object_get_type(poError) == json_type_string ) { CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s", json_object_get_string(poError)); json_object_put(poObj); return NULL; } } } else { json_object_put(poObj); return NULL; } } return poObj; }
json_object* OGRAmigoCloudDataSource::RunSQL(const char* pszUnescapedSQL) { CPLString osSQL; osSQL = "/users/0/projects/" + CPLString(pszProjetctId) + "/sql"; /* -------------------------------------------------------------------- */ /* Provide the API Key */ /* -------------------------------------------------------------------- */ if( osAPIKey.size() > 0 ) { osSQL += "?token="; osSQL += osAPIKey; } osSQL += "&query="; char * pszEscaped = CPLEscapeString( pszUnescapedSQL, -1, CPLES_URL ); std::string escaped = pszEscaped; CPLFree( pszEscaped ); osSQL += escaped; /* -------------------------------------------------------------------- */ /* Collection the header options and execute request. */ /* -------------------------------------------------------------------- */ std::string pszAPIURL = GetAPIURL(); char** papszOptions = NULL; pszAPIURL += osSQL; CPLHTTPResult * psResult = CPLHTTPFetch( pszAPIURL.c_str(), papszOptions); CSLDestroy(papszOptions); if( psResult == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Check for some error conditions and report. HTML Messages */ /* are transformed info failure. */ /* -------------------------------------------------------------------- */ if (psResult->pszContentType && strncmp(psResult->pszContentType, "text/html", 9) == 0) { CPLDebug( "AMIGOCLOUD", "RunSQL HTML Response:%s", psResult->pabyData ); CPLError(CE_Failure, CPLE_AppDefined, "HTML error page returned by server"); CPLHTTPDestroyResult(psResult); return NULL; } if (psResult->pszErrBuf != NULL) { CPLDebug( "AMIGOCLOUD", "RunSQL Error Message:%s", psResult->pszErrBuf ); } else if (psResult->nStatus != 0) { CPLDebug( "AMIGOCLOUD", "RunSQL Error Status:%d", psResult->nStatus ); } if( psResult->pabyData == NULL ) { CPLHTTPDestroyResult(psResult); return NULL; } CPLDebug( "AMIGOCLOUD", "RunSQL Response:%s", psResult->pabyData ); json_tokener* jstok = NULL; json_object* poObj = NULL; jstok = json_tokener_new(); poObj = json_tokener_parse_ex(jstok, (const char*) psResult->pabyData, -1); if( jstok->err != json_tokener_success) { CPLError( CE_Failure, CPLE_AppDefined, "JSON parsing error: %s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset); json_tokener_free(jstok); CPLHTTPDestroyResult(psResult); return NULL; } json_tokener_free(jstok); CPLHTTPDestroyResult(psResult); if( poObj != NULL ) { if( json_object_get_type(poObj) == json_type_object ) { json_object* poError = CPL_json_object_object_get(poObj, "error"); if( poError != NULL && json_object_get_type(poError) == json_type_array && json_object_array_length(poError) > 0 ) { poError = json_object_array_get_idx(poError, 0); if( poError != NULL && json_object_get_type(poError) == json_type_string ) { CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s", json_object_get_string(poError)); json_object_put(poObj); return NULL; } } } else { json_object_put(poObj); return NULL; } } return poObj; }
int OGRGFTTableLayer::FetchDescribe() { poFeatureDefn = new OGRFeatureDefn( osTableName ); poFeatureDefn->Reference(); poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poSRS); const CPLString& osAuth = poDS->GetAccessToken(); std::vector<CPLString> aosHeaderAndFirstDataLine; if (osAuth.size()) { CPLString osSQL("DESCRIBE "); osSQL += osTableId; CPLHTTPResult * psResult = poDS->RunSQL(osSQL); if (psResult == NULL) return FALSE; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL || strncmp(pszLine, "column id,name,type", strlen("column id,name,type")) != 0) { CPLHTTPDestroyResult(psResult); return FALSE; } pszLine = OGRGFTGotoNextLine(pszLine); std::vector<CPLString> aosLines; ParseCSVResponse(pszLine, aosLines); for(int i=0;i<(int)aosLines.size();i++) { char** papszTokens = OGRGFTCSVSplitLine(aosLines[i], ','); if (CSLCount(papszTokens) == 3) { aosColumnInternalName.push_back(papszTokens[0]); //CPLDebug("GFT", "%s %s %s", papszTokens[0], papszTokens[1], papszTokens[2]); OGRFieldType eType = OFTString; if (EQUAL(papszTokens[2], "number")) eType = OFTReal; else if (EQUAL(papszTokens[2], "datetime")) eType = OFTDateTime; if (EQUAL(papszTokens[2], "location") && osGeomColumnName.size() == 0) { if (iGeometryField < 0) iGeometryField = poFeatureDefn->GetFieldCount(); else CPLDebug("GFT", "Multiple geometry fields detected. " "Only first encountered one is handled"); } CPLString osLaunderedColName(LaunderColName(papszTokens[1])); OGRFieldDefn oFieldDefn(osLaunderedColName, eType); poFeatureDefn->AddFieldDefn(&oFieldDefn); } CSLDestroy(papszTokens); } CPLHTTPDestroyResult(psResult); } else { /* http://code.google.com/intl/fr/apis/fusiontables/docs/developers_guide.html#Exploring states */ /* that DESCRIBE should work on public tables without authentication, but it is not true... */ CPLString osSQL("SELECT * FROM "); osSQL += osTableId; osSQL += " OFFSET 0 LIMIT 1"; CPLHTTPResult * psResult = poDS->RunSQL(osSQL); if (psResult == NULL) return FALSE; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL) { CPLHTTPDestroyResult(psResult); return FALSE; } ParseCSVResponse(pszLine, aosHeaderAndFirstDataLine); if (aosHeaderAndFirstDataLine.size() > 0) { char** papszTokens = OGRGFTCSVSplitLine(aosHeaderAndFirstDataLine[0], ','); for(int i=0;papszTokens && papszTokens[i];i++) { CPLString osLaunderedColName(LaunderColName(papszTokens[i])); OGRFieldDefn oFieldDefn(osLaunderedColName, OFTString); poFeatureDefn->AddFieldDefn(&oFieldDefn); } CSLDestroy(papszTokens); } CPLHTTPDestroyResult(psResult); } if (osGeomColumnName.size() > 0) { iGeometryField = poFeatureDefn->GetFieldIndex(osGeomColumnName); if (iGeometryField < 0) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot find column called %s", osGeomColumnName.c_str()); } } for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { const char* pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); if (EQUAL(pszName, "latitude") || EQUAL(pszName, "lat") || EQUAL(pszName, "latdec")) iLatitudeField = i; else if (EQUAL(pszName, "longitude") || EQUAL(pszName, "lon") || EQUAL(pszName, "londec") || EQUAL(pszName, "long")) iLongitudeField = i; } if (iLatitudeField >= 0 && iLongitudeField >= 0) { if (iGeometryField < 0) iGeometryField = iLatitudeField; poFeatureDefn->GetFieldDefn(iLatitudeField)->SetType(OFTReal); poFeatureDefn->GetFieldDefn(iLongitudeField)->SetType(OFTReal); poFeatureDefn->SetGeomType( wkbPoint ); } else if (iGeometryField < 0 && osGeomColumnName.size() == 0) { iLatitudeField = iLongitudeField = -1; /* In the unauthentified case, we try to parse the first record to */ /* autodetect the geometry field */ OGRwkbGeometryType eType = wkbUnknown; if (aosHeaderAndFirstDataLine.size() == 2) { char** papszTokens = OGRGFTCSVSplitLine(aosHeaderAndFirstDataLine[1], ','); if (CSLCount(papszTokens) == poFeatureDefn->GetFieldCount()) { for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { const char* pszVal = papszTokens[i]; if (pszVal != NULL && (strncmp(pszVal, "<Point>", 7) == 0 || strncmp(pszVal, "<LineString>", 12) == 0 || strncmp(pszVal, "<Polygon>", 9) == 0 || strncmp(pszVal, "<MultiGeometry>", 15) == 0)) { if (iGeometryField < 0) { iGeometryField = i; } else { CPLDebug("GFT", "Multiple geometry fields detected. " "Only first encountered one is handled"); } } else if (pszVal) { /* http://www.google.com/fusiontables/DataSource?dsrcid=423292 */ char** papszTokens2 = CSLTokenizeString2(pszVal, " ,", 0); if (CSLCount(papszTokens2) == 2 && CPLGetValueType(papszTokens2[0]) == CPL_VALUE_REAL && CPLGetValueType(papszTokens2[1]) == CPL_VALUE_REAL && fabs(CPLAtof(papszTokens2[0])) <= 90 && fabs(CPLAtof(papszTokens2[1])) <= 180 ) { if (iGeometryField < 0) { iGeometryField = i; eType = wkbPoint; } else { CPLDebug("GFT", "Multiple geometry fields detected. " "Only first encountered one is handled"); } } CSLDestroy(papszTokens2); } } } CSLDestroy(papszTokens); } if (iGeometryField < 0) poFeatureDefn->SetGeomType( wkbNone ); else poFeatureDefn->SetGeomType( eType ); } SetGeomFieldName(); return TRUE; }
OGRErr OGRGFTTableLayer::DeleteFeature( GIntBig nFID ) { GetLayerDefn(); if (!poDS->IsReadWrite()) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in read-only mode"); return OGRERR_FAILURE; } if (osTableId.size() == 0) { CPLError(CE_Failure, CPLE_NotSupported, "Cannot delete feature in non-created table"); return OGRERR_FAILURE; } if (poDS->GetAccessToken().size() == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in unauthenticated mode"); return OGRERR_FAILURE; } CPLString osCommand; osCommand += "DELETE FROM "; osCommand += osTableId; osCommand += " WHERE ROWID = '"; osCommand += CPLSPrintf(CPL_FRMT_GIB, nFID); osCommand += "'"; //CPLDebug("GFT", "%s", osCommand.c_str()); CPLHTTPResult * psResult = poDS->RunSQL(osCommand); if (psResult == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Feature deletion failed (1)"); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* We expect a response like "affected_rows\n1". */ /* -------------------------------------------------------------------- */ char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "affected_rows\n1\n", 16) != 0 || psResult->pszErrBuf != NULL) { CPLDebug( "GFT", "%s/%s", pszLine ? pszLine : "null", psResult->pszErrBuf ? psResult->pszErrBuf : "null"); CPLError(CE_Failure, CPLE_AppDefined, "Feature deletion failed (2)"); CPLHTTPDestroyResult(psResult); return OGRERR_FAILURE; } CPLHTTPDestroyResult(psResult); return OGRERR_NONE; }
int OGRGFTResultLayer::RunSQL() { CPLString osChangedSQL(osSQL); int bHasSetLimit = FALSE; OGRGFTTableLayer* poTableLayer = NULL; OGRFeatureDefn* poTableDefn = NULL; CPLString osTableId; if (EQUALN(osSQL.c_str(), "SELECT", 6)) { size_t nPosFROM = osSQL.ifind(" FROM "); if (nPosFROM == std::string::npos) { CPLError(CE_Failure, CPLE_AppDefined, "RunSQL() failed. Missing FROM in SELECT"); return FALSE; } CPLString osReminder; nPosFROM += 6; osTableId = OGRGFTExtractTableID(osSQL.c_str() + nPosFROM, osReminder); poTableLayer = (OGRGFTTableLayer*) poDS->GetLayerByName(osTableId); if (poTableLayer != NULL) poTableDefn = poTableLayer->GetLayerDefn(); if (poTableLayer != NULL && poTableLayer->GetTableId().size() && !EQUAL(osTableId, poTableLayer->GetTableId())) { osChangedSQL = osSQL; osChangedSQL.resize(nPosFROM); osChangedSQL += poTableLayer->GetTableId(); osChangedSQL += osReminder; osSQL = osChangedSQL; CPLDebug("GFT", "Patching table name (%s) to table id (%s)", osTableId.c_str(), poTableLayer->GetTableId().c_str()); } int nFeaturesToFetch = GetFeaturesToFetch(); if (osSQL.ifind(" OFFSET ") == std::string::npos && osSQL.ifind(" LIMIT ") == std::string::npos && nFeaturesToFetch > 0) { osChangedSQL += CPLSPrintf(" LIMIT %d", nFeaturesToFetch); bHasSetLimit = TRUE; } } else { bGotAllRows = bEOF = TRUE; poFeatureDefn->SetGeomType( wkbNone ); } CPLHTTPResult * psResult = poDS->RunSQL(osChangedSQL); if (psResult == NULL) return FALSE; char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "RunSQL() failed"); CPLHTTPDestroyResult(psResult); return FALSE; } if (EQUALN(osSQL.c_str(), "SELECT", 6) || EQUAL(osSQL.c_str(), "SHOW TABLES") || EQUALN(osSQL.c_str(), "DESCRIBE", 8)) { ParseCSVResponse(pszLine, aosRows); if (aosRows.size() > 0) { char** papszTokens = OGRGFTCSVSplitLine(aosRows[0], ','); for(int i=0;papszTokens && papszTokens[i];i++) { CPLString osLaunderedColName(LaunderColName(papszTokens[i])); int iIndex = (poTableDefn) ? poTableDefn->GetFieldIndex(osLaunderedColName) : -1; if (iIndex >= 0) { poFeatureDefn->AddFieldDefn(poTableDefn->GetFieldDefn(iIndex)); if (iIndex == poTableLayer->GetGeometryFieldIndex()) iGeometryField = i; if (iIndex == poTableLayer->GetLatitudeFieldIndex()) iLatitudeField = i; if (iIndex == poTableLayer->GetLongitudeFieldIndex()) iLongitudeField = i; } else { OGRFieldType eType = OFTString; if (EQUAL(osLaunderedColName, "COUNT()")) eType = OFTInteger; OGRFieldDefn oFieldDefn(osLaunderedColName, eType); poFeatureDefn->AddFieldDefn(&oFieldDefn); } } CSLDestroy(papszTokens); aosRows.erase(aosRows.begin()); } if (iLatitudeField >= 0 && iLongitudeField >= 0) { iGeometryField = iLatitudeField; poFeatureDefn->SetGeomType( wkbPoint ); } if (bHasSetLimit) bGotAllRows = bEOF = (int)aosRows.size() < GetFeaturesToFetch(); else bGotAllRows = bEOF = TRUE; } SetGeomFieldName(); CPLHTTPDestroyResult(psResult); return TRUE; }
OGRErr OGRGFTTableLayer::ISetFeature( OGRFeature *poFeature ) { GetLayerDefn(); if (!poDS->IsReadWrite()) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in read-only mode"); return OGRERR_FAILURE; } if (osTableId.size() == 0) { CPLError(CE_Failure, CPLE_NotSupported, "Cannot set feature to non-created table"); return OGRERR_FAILURE; } if (poDS->GetAccessToken().size() == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in unauthenticated mode"); return OGRERR_FAILURE; } if (poFeature->GetFID() == OGRNullFID) { CPLError( CE_Failure, CPLE_AppDefined, "FID required on features given to SetFeature()." ); return OGRERR_FAILURE; } CPLString osCommand; osCommand += "UPDATE "; osCommand += osTableId; osCommand += " SET "; int iField; int nFieldCount = poFeatureDefn->GetFieldCount(); for(iField = 0; iField < nFieldCount + bHiddenGeometryField; iField++) { if (iField > 0) osCommand += ", "; if (iField == nFieldCount) { osCommand += EscapeAndQuote(GetGeometryColumn()); } else { const char* pszFieldName = poFeatureDefn->GetFieldDefn(iField)->GetNameRef(); osCommand += EscapeAndQuote(pszFieldName); } osCommand += " = "; OGRGeometry* poGeom = poFeature->GetGeometryRef(); if (iGeometryField != iLatitudeField && iField == iGeometryField && (iField == nFieldCount || poGeom != NULL || !poFeature->IsFieldSet( iField ))) { if (poGeom == NULL) osCommand += "''"; else { char* pszKML; if (poGeom->getSpatialReference() != NULL && !poGeom->getSpatialReference()->IsSame(poSRS)) { OGRGeometry* poGeom4326 = poGeom->clone(); poGeom4326->transformTo(poSRS); pszKML = poGeom4326->exportToKML(); delete poGeom4326; } else { pszKML = poGeom->exportToKML(); } osCommand += "'"; osCommand += pszKML; osCommand += "'"; CPLFree(pszKML); } continue; } if( !poFeature->IsFieldSet( iField ) ) { osCommand += "''"; } else { OGRFieldType eType = poFeatureDefn->GetFieldDefn(iField)->GetType(); if (eType != OFTInteger && eType != OFTReal) { CPLString osTmp; const char* pszVal = poFeature->GetFieldAsString(iField); if (!CPLIsUTF8(pszVal, -1)) { static int bFirstTime = TRUE; if (bFirstTime) { bFirstTime = FALSE; CPLError(CE_Warning, CPLE_AppDefined, "%s is not a valid UTF-8 string. Forcing it to ASCII.\n" "This warning won't be issued anymore", pszVal); } else { CPLDebug("OGR", "%s is not a valid UTF-8 string. Forcing it to ASCII", pszVal); } char* pszEscaped = CPLForceToASCII(pszVal, -1, '?'); osTmp = pszEscaped; CPLFree(pszEscaped); pszVal = osTmp.c_str(); } osCommand += EscapeAndQuote(pszVal); } else osCommand += poFeature->GetFieldAsString(iField); } } osCommand += " WHERE ROWID = '"; osCommand += CPLSPrintf(CPL_FRMT_GIB, poFeature->GetFID()); osCommand += "'"; CPLHTTPResult * psResult = poDS->RunSQL(osCommand); if (psResult == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Feature update failed (1)"); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* We expect a response like "affected_rows\n1". */ /* -------------------------------------------------------------------- */ char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "affected_rows\n1\n", 16) != 0 || psResult->pszErrBuf != NULL) { CPLDebug( "GFT", "%s/%s", pszLine ? pszLine : "null", psResult->pszErrBuf ? psResult->pszErrBuf : "null"); CPLError(CE_Failure, CPLE_AppDefined, "Feature update failed (2)"); CPLHTTPDestroyResult(psResult); return OGRERR_FAILURE; } CPLHTTPDestroyResult(psResult); return OGRERR_NONE; }
OGRErr OGRGFTTableLayer::ICreateFeature( OGRFeature *poFeature ) { if (!poDS->IsReadWrite()) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in read-only mode"); return OGRERR_FAILURE; } if (osTableId.size() == 0) { CreateTableIfNecessary(); if (osTableId.size() == 0) { CPLError(CE_Failure, CPLE_NotSupported, "Cannot add feature to non-created table"); return OGRERR_FAILURE; } } if (poDS->GetAccessToken().size() == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Operation not available in unauthenticated mode"); return OGRERR_FAILURE; } CPLString osCommand; osCommand += "INSERT INTO "; osCommand += osTableId; osCommand += " ("; int iField; int nFieldCount = poFeatureDefn->GetFieldCount(); for(iField = 0; iField < nFieldCount; iField++) { if (iField > 0) osCommand += ", "; const char* pszFieldName = poFeatureDefn->GetFieldDefn(iField)->GetNameRef(); osCommand += EscapeAndQuote(pszFieldName); } if (bHiddenGeometryField) { if (iField > 0) osCommand += ", "; osCommand += EscapeAndQuote(GetGeometryColumn()); } osCommand += ") VALUES ("; for(iField = 0; iField < nFieldCount + bHiddenGeometryField; iField++) { if (iField > 0) osCommand += ", "; OGRGeometry* poGeom = poFeature->GetGeometryRef(); /* If there's a geometry, let's use it in priority over the textual */ /* content of the field. */ if (iGeometryField != iLatitudeField && iField == iGeometryField && (iField == nFieldCount || poGeom != NULL || !poFeature->IsFieldSet( iField ))) { if (poGeom == NULL) osCommand += "''"; else { char* pszKML; if (poGeom->getSpatialReference() != NULL && !poGeom->getSpatialReference()->IsSame(poSRS)) { OGRGeometry* poGeom4326 = poGeom->clone(); poGeom4326->transformTo(poSRS); pszKML = poGeom4326->exportToKML(); delete poGeom4326; } else { pszKML = poGeom->exportToKML(); } osCommand += "'"; osCommand += pszKML; osCommand += "'"; CPLFree(pszKML); } continue; } if( !poFeature->IsFieldSet( iField ) ) { osCommand += "''"; } else { OGRFieldType eType = poFeatureDefn->GetFieldDefn(iField)->GetType(); if (eType != OFTInteger && eType != OFTReal) { CPLString osTmp; const char* pszVal = poFeature->GetFieldAsString(iField); if (!CPLIsUTF8(pszVal, -1)) { static int bFirstTime = TRUE; if (bFirstTime) { bFirstTime = FALSE; CPLError(CE_Warning, CPLE_AppDefined, "%s is not a valid UTF-8 string. Forcing it to ASCII.\n" "This warning won't be issued anymore", pszVal); } else { CPLDebug("OGR", "%s is not a valid UTF-8 string. Forcing it to ASCII", pszVal); } char* pszEscaped = CPLForceToASCII(pszVal, -1, '?'); osTmp = pszEscaped; CPLFree(pszEscaped); pszVal = osTmp.c_str(); } osCommand += EscapeAndQuote(pszVal); } else osCommand += poFeature->GetFieldAsString(iField); } } osCommand += ")"; //CPLDebug("GFT", "%s", osCommand.c_str()); if (bInTransaction) { nFeaturesInTransaction ++; if (nFeaturesInTransaction > 1) osTransaction += "; "; osTransaction += osCommand; return OGRERR_NONE; } CPLHTTPResult * psResult = poDS->RunSQL(osCommand); if (psResult == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Feature creation failed"); return OGRERR_FAILURE; } char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "rowid", 5) != 0 || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Feature creation failed"); CPLHTTPDestroyResult(psResult); return OGRERR_FAILURE; } pszLine = OGRGFTGotoNextLine(pszLine); if (pszLine == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Feature creation failed"); CPLHTTPDestroyResult(psResult); return OGRERR_FAILURE; } char* pszNextLine = OGRGFTGotoNextLine(pszLine); if (pszNextLine) pszNextLine[-1] = 0; CPLDebug("GFT", "Feature id = %s", pszLine); int nFID = atoi(pszLine); if (strcmp(CPLSPrintf("%d", nFID), pszLine) == 0) poFeature->SetFID(nFID); CPLHTTPDestroyResult(psResult); return OGRERR_NONE; }
void OGRGFTTableLayer::CreateTableIfNecessary() { if (bHasTriedCreateTable || osTableId.size() != 0) return; bHasTriedCreateTable = TRUE; CPLString osSQL("CREATE TABLE '"); osSQL += osTableName; osSQL += "' ("; int i; /* If there are longitude and latitude fields, use the latitude */ /* field as the LOCATION field */ for(i=0;i<poFeatureDefn->GetFieldCount();i++) { const char* pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); if (EQUAL(pszName, "latitude") || EQUAL(pszName, "lat") || EQUAL(pszName, "latdec")) iLatitudeField = i; else if (EQUAL(pszName, "longitude") || EQUAL(pszName, "lon") || EQUAL(pszName, "londec") || EQUAL(pszName, "long")) iLongitudeField = i; } if (iLatitudeField >= 0 && iLongitudeField >= 0) { iGeometryField = iLatitudeField; poFeatureDefn->SetGeomType( wkbPoint ); } /* If no longitude/latitude field exist, let's look at a column */ /* named 'geometry' and use it as the LOCATION column if the layer */ /* hasn't been created with a none geometry type */ else if (iGeometryField < 0 && eGTypeForCreation != wkbNone) { iGeometryField = poFeatureDefn->GetFieldIndex(GetDefaultGeometryColumnName()); poFeatureDefn->SetGeomType( eGTypeForCreation ); } /* The user doesn't want geometries, so don't create one */ else if (eGTypeForCreation == wkbNone) { poFeatureDefn->SetGeomType( eGTypeForCreation ); } for(i=0;i<poFeatureDefn->GetFieldCount();i++) { if (i > 0) osSQL += ", "; const char* pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); osSQL += EscapeAndQuote(pszFieldName); osSQL += ": "; if (iGeometryField == i) { osSQL += "LOCATION"; } else { switch(poFeatureDefn->GetFieldDefn(i)->GetType()) { case OFTInteger: case OFTReal: osSQL += "NUMBER"; break; default: osSQL += "STRING"; } } } /* If there's not yet a geometry field and the user didn't forbid */ /* the creation of one, then let's add it to the CREATE TABLE, but */ /* DO NOT add it to the feature defn as a feature might already have */ /* been created with it, so it is not safe to alter it at that point. */ /* So we set the bHiddenGeometryField flag to be able to fetch/set this */ /* column but not try to get/set a related feature field */ if (iGeometryField < 0 && eGTypeForCreation != wkbNone) { if (i > 0) osSQL += ", "; osSQL += EscapeAndQuote(GetDefaultGeometryColumnName()); osSQL += ": LOCATION"; iGeometryField = poFeatureDefn->GetFieldCount(); bHiddenGeometryField = TRUE; } osSQL += ")"; CPLHTTPResult * psResult = poDS->RunSQL(osSQL); if (psResult == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Table creation failed"); return; } char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || strncmp(pszLine, "tableid", 7) != 0 || psResult->pszErrBuf != NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Table creation failed"); CPLHTTPDestroyResult(psResult); return; } pszLine = OGRGFTGotoNextLine(pszLine); if (pszLine == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Table creation failed"); CPLHTTPDestroyResult(psResult); return; } char* pszNextLine = OGRGFTGotoNextLine(pszLine); if (pszNextLine) pszNextLine[-1] = 0; osTableId = pszLine; CPLDebug("GFT", "Table %s --> id = %s", osTableName.c_str(), osTableId.c_str()); CPLHTTPDestroyResult(psResult); }
int OGRGFTTableLayer::FetchNextRows() { aosRows.resize(0); CPLString osSQL("SELECT ROWID"); for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { osSQL += ","; if (i < (int)aosColumnInternalName.size()) osSQL += aosColumnInternalName[i]; else { const char* pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); osSQL += EscapeAndQuote(pszFieldName); } } if (bHiddenGeometryField) { osSQL += ","; osSQL += EscapeAndQuote(GetGeometryColumn()); } osSQL += " FROM "; osSQL += osTableId; if (osWHERE.size()) { osSQL += " "; osSQL += osWHERE; } int nFeaturesToFetch = GetFeaturesToFetch(); if (nFeaturesToFetch > 0) osSQL += CPLSPrintf(" OFFSET %d LIMIT %d", nOffset, nFeaturesToFetch); CPLPushErrorHandler(CPLQuietErrorHandler); CPLHTTPResult * psResult = poDS->RunSQL(osSQL); CPLPopErrorHandler(); if (psResult == NULL) { bEOF = TRUE; return FALSE; } char* pszLine = (char*) psResult->pabyData; if (pszLine == NULL || psResult->pszErrBuf != NULL) { CPLDebug("GFT", "Error : %s", pszLine ? pszLine : psResult->pszErrBuf); CPLHTTPDestroyResult(psResult); bEOF = TRUE; return FALSE; } ParseCSVResponse(pszLine, aosRows); if (aosRows.size() > 0) aosRows.erase(aosRows.begin()); if (nFeaturesToFetch > 0) bEOF = (int)aosRows.size() < GetFeaturesToFetch(); else bEOF = TRUE; CPLHTTPDestroyResult(psResult); return TRUE; }