char* OGRGetXML_UTF8_EscapedString(const char* pszString) { char *pszEscaped; if (!CPLIsUTF8(pszString, -1) && CSLTestBoolean(CPLGetConfigOption("OGR_FORCE_ASCII", "YES"))) { 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" "If you still want the original string and change the XML file encoding\n" "afterwards, you can define OGR_FORCE_ASCII=NO as configuration option.\n" "This warning won't be issued anymore", pszString); } else { CPLDebug("OGR", "%s is not a valid UTF-8 string. Forcing it to ASCII", pszString); } char* pszTemp = CPLForceToASCII(pszString, -1, '?'); pszEscaped = CPLEscapeString( pszTemp, -1, CPLES_XML ); CPLFree(pszTemp); } else pszEscaped = CPLEscapeString( pszString, -1, CPLES_XML ); return pszEscaped; }
static GDALDataset *OGRSUADriverOpen( GDALOpenInfo* poOpenInfo ) { if( poOpenInfo->eAccess == GA_Update || poOpenInfo->fpL == NULL || !poOpenInfo->TryToIngest(10000) ) return NULL; int bIsSUA = ( strstr((const char*)poOpenInfo->pabyHeader, "\nTYPE=") != NULL && strstr((const char*)poOpenInfo->pabyHeader, "\nTITLE=") != NULL && (strstr((const char*)poOpenInfo->pabyHeader, "\nPOINT=") != NULL || strstr((const char*)poOpenInfo->pabyHeader, "\nCIRCLE ") != NULL)); if( !bIsSUA ) { /* Some files such http://soaringweb.org/Airspace/CZ/CZ_combined_2014_05_01.sua */ /* have very long comments in the header, so we will have to check */ /* further, but only do this is we have a hint that the file might be */ /* a candidate */ int nLen = poOpenInfo->nHeaderBytes; if( nLen < 10000 ) return NULL; /* Check the 'Airspace' word in the header */ if( strstr((const char*)poOpenInfo->pabyHeader, "Airspace") == NULL ) return NULL; // Check that the header is at least UTF-8 // but do not take into account partial UTF-8 characters at the end int nTruncated = 0; while(nLen > 0) { if( (poOpenInfo->pabyHeader[nLen-1] & 0xc0) != 0x80 ) { break; } nLen --; nTruncated ++; if( nTruncated == 7 ) return NULL; } if( !CPLIsUTF8((const char*)poOpenInfo->pabyHeader, nLen) ) return NULL; if( !poOpenInfo->TryToIngest(30000) ) return NULL; bIsSUA = ( strstr((const char*)poOpenInfo->pabyHeader, "\nTYPE=") != NULL && strstr((const char*)poOpenInfo->pabyHeader, "\nTITLE=") != NULL && (strstr((const char*)poOpenInfo->pabyHeader, "\nPOINT=") != NULL || strstr((const char*)poOpenInfo->pabyHeader, "\nCIRCLE ") != NULL) ); if( !bIsSUA ) return NULL; } OGRSUADataSource *poDS = new OGRSUADataSource(); if( !poDS->Open( poOpenInfo->pszFilename ) ) { delete poDS; poDS = NULL; } return poDS; }
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; }
static GDALDataset *OGROpenAirDriverOpen( GDALOpenInfo* poOpenInfo ) { if( poOpenInfo->eAccess == GA_Update || poOpenInfo->fpL == NULL || !poOpenInfo->TryToIngest(10000) ) return NULL; const char *pabyHeader = reinterpret_cast<char *>(poOpenInfo->pabyHeader); bool bIsOpenAir = strstr(pabyHeader, "\nAC ") != NULL && strstr(pabyHeader, "\nAN ") != NULL && strstr(pabyHeader, "\nAL ") != NULL && strstr(pabyHeader, "\nAH") != NULL; if( !bIsOpenAir ) { // Some files, such as // http://soaringweb.org/Airspace/CZ/CZ_combined_2014_05_01.txt , // have very long comments in the header, so we will have to // check further, but only do this is we have a hint that the // file might be a candidate. int nLen = poOpenInfo->nHeaderBytes; if( nLen < 10000 ) return NULL; /* Check the 'Airspace' word in the header */ if( strstr(pabyHeader, "Airspace") == NULL ) return NULL; // Check that the header is at least UTF-8 // but do not take into account partial UTF-8 characters at the end int nTruncated = 0; while(nLen > 0) { if( (poOpenInfo->pabyHeader[nLen-1] & 0xc0) != 0x80 ) { break; } nLen --; nTruncated ++; if( nTruncated == 7 ) return NULL; } if( !CPLIsUTF8(pabyHeader, nLen) ) return NULL; if( !poOpenInfo->TryToIngest(30000) ) return NULL; bIsOpenAir = strstr(pabyHeader, "\nAC ") != NULL && strstr(pabyHeader, "\nAN ") != NULL && strstr(pabyHeader, "\nAL ") != NULL && strstr(pabyHeader, "\nAH") != NULL; if( !bIsOpenAir ) return NULL; } OGROpenAirDataSource *poDS = new OGROpenAirDataSource(); if( !poDS->Open( poOpenInfo->pszFilename ) ) { delete poDS; poDS = NULL; } return poDS; }
SEXP ogrReadColumn(OGRLayer *poLayer, SEXP FIDs, int iField, int int64, int ENC_DEBUG) { // read feature data and return something according to the type OGRFeatureDefn *poDefn; OGRFieldDefn *poField; OGRFeature *poFeature; int iRow,nRows; SEXP ans = R_NilValue; nRows=length(FIDs); // get field data from layer installErrorHandler(); poDefn = poLayer->GetLayerDefn(); poField = poDefn->GetFieldDefn(iField); uninstallErrorHandlerAndTriggerError(); if(poField == NULL) { error("Error getting field %d ",iField); } // allocate an object for the result depending on the feature type: installErrorHandler(); switch(poField->GetType()) { case OFTInteger: PROTECT(ans=allocVector(INTSXP,nRows)); break; #ifdef GDALV2 case OFTInteger64: if (int64 ==3) { PROTECT(ans=allocVector(STRSXP,nRows)); } else { PROTECT(ans=allocVector(INTSXP,nRows)); } break; #endif case OFTReal: PROTECT(ans=allocVector(REALSXP,nRows)); break; case OFTString: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTDate: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTDateTime: PROTECT(ans=allocVector(STRSXP,nRows)); break; case OFTTime: PROTECT(ans=allocVector(STRSXP,nRows)); break; default: const char *desc = poField->GetFieldTypeName(poField->GetType()); uninstallErrorHandlerAndTriggerError(); error("unsupported field type: %s", desc); break; } uninstallErrorHandlerAndTriggerError(); // now go over each row and retrieve data. iRow is an index in a // vector of FIDs /*#ifndef EJP installErrorHandler(); for(iRow=0;iRow<nRows;iRow++){ poFeature=poLayer->GetFeature(INTEGER(FIDs)[iRow]); if(poFeature == NULL){ error("Error getting feature FID: %d",(INTEGER(FIDs)[iRow])); } } uninstallErrorHandlerAndTriggerError(); #else*/ // EJP, changed into: installErrorHandler(); poLayer->ResetReading(); iRow = 0; while((poFeature = poLayer->GetNextFeature()) != NULL) { //#endif // now get the value using the right type: switch(poField->GetType()) { case OFTInteger: if (poFeature->IsFieldSet(iField)) INTEGER(ans)[iRow]=poFeature->GetFieldAsInteger(iField); else INTEGER(ans)[iRow]=NA_INTEGER; break; #ifdef GDALV2 case OFTInteger64: if (poFeature->IsFieldSet(iField)) { if (int64 == 3) { SET_STRING_ELT(ans, iRow, mkChar(poFeature->GetFieldAsString(iField))); } else { GIntBig nVal64 = poFeature->GetFieldAsInteger64(iField); int nVal = (nVal64 > INT_MAX) ? INT_MAX : (nVal64 < INT_MIN) ? INT_MIN : (int) nVal64; INTEGER(ans)[iRow]=nVal; if (((GIntBig)nVal != nVal64) && int64 == 2) { warning("Integer64 value clamped: feature %d", iRow); } } } else { if (int64 == 3) { SET_STRING_ELT(ans, iRow, NA_STRING); } else { INTEGER(ans)[iRow]=NA_INTEGER; } } break; #endif case OFTReal: if (poFeature->IsFieldSet(iField)) REAL(ans)[iRow]=poFeature->GetFieldAsDouble(iField); else REAL(ans)[iRow]=NA_REAL; break; case OFTString: // ENC char str[4096]; size_t stln; if (poFeature->IsFieldSet(iField)) { stln = CPLStrnlen(poFeature->GetFieldAsString(iField), 4096); CPLStrlcpy(str, (const char *) poFeature->GetFieldAsString(iField), 4096); SET_STRING_ELT(ans, iRow, mkChar(str)); } else SET_STRING_ELT(ans, iRow, NA_STRING); if (ENC_DEBUG) { Rprintf("iField: %d, iRow: %d stln %u Enc %s ", iField, iRow, stln, CPLIsUTF8(str, (int) stln)?"UTF-8":"other"); for (int si=0; si < (int) stln; si++) Rprintf("%x ", (unsigned char) str[si]); Rprintf("\n"); } /* FIXME */ break; case OFTDate: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; case OFTDateTime: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; case OFTTime: if (poFeature->IsFieldSet(iField)) SET_STRING_ELT(ans,iRow,mkChar(poFeature->GetFieldAsString(iField))); else SET_STRING_ELT(ans, iRow, NA_STRING); break; default: OGRFeature::DestroyFeature( poFeature ); // delete poFeature; uninstallErrorHandlerAndTriggerError(); error("Unsupported field type. should have been caught before"); } OGRFeature::DestroyFeature( poFeature ); // delete poFeature; //#ifdef EJP // according to tutorial: OGRFeature::DestroyFeature(poFeature); // see comment FW in OGR tutorial: We could just "delete" it, // but this can cause problems in windows builds where the GDAL DLL // has a different "heap" from the main program. To be on the safe // side we use a GDAL function to delete the feature. iRow++; //#endif } uninstallErrorHandlerAndTriggerError(); UNPROTECT(1); return(ans); }