const char* E00GRIDDataset::ReadLine() { if (e00ReadPtr) return E00ReadNextLine(e00ReadPtr); return CPLReadLine2L(fp, 81, NULL); }
int OGRHTFSoundingLayer::GetFeatureCount(int bForce) { if (m_poFilterGeom != NULL || m_poAttrQuery != NULL) return OGRHTFLayer::GetFeatureCount(bForce); if (nTotalSoundings != 0) return nTotalSoundings; ResetReading(); if (fpHTF == NULL) return 0; int nCount = 0; const char* pszLine; while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (pszLine[0] == ';') { /* comment */ ; } else if (pszLine[0] == 0) break; else if (strcmp(pszLine, "END OF SOUNDING DATA") == 0) break; else nCount ++; } ResetReading(); return nCount; }
OGRFeature *OGROpenAirLabelLayer::GetNextRawFeature() { const char* pszLine; double dfLat = 0, dfLon = 0; int bHasCoord = FALSE; while(TRUE) { pszLine = CPLReadLine2L(fpOpenAir, 1024, NULL); if (pszLine == NULL) return NULL; if (pszLine[0] == '*' || pszLine[0] == '\0') continue; if (EQUALN(pszLine, "AC ", 3)) { if (osCLASS.size() != 0) { osNAME = ""; osCEILING = ""; osFLOOR = ""; } osCLASS = pszLine + 3; } else if (EQUALN(pszLine, "AN ", 3)) osNAME = pszLine + 3; else if (EQUALN(pszLine, "AH ", 3)) osCEILING = pszLine + 3; else if (EQUALN(pszLine, "AL ", 3)) osFLOOR = pszLine + 3; else if (EQUALN(pszLine, "AT ", 3)) { bHasCoord = OGROpenAirGetLatLon(pszLine + 3, dfLat, dfLon); break; } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetField(0, osCLASS.c_str()); poFeature->SetField(1, osNAME.c_str()); poFeature->SetField(2, osFLOOR.c_str()); poFeature->SetField(3, osCEILING.c_str()); CPLString osStyle; osStyle.Printf("LABEL(t:\"%s\")", osNAME.c_str()); poFeature->SetStyleString(osStyle.c_str()); if (bHasCoord) { OGRPoint* poPoint = new OGRPoint(dfLon, dfLat); poPoint->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoint); } poFeature->SetFID(nNextFID++); return poFeature; }
void OGRHTFSoundingLayer::ResetReading() { OGRHTFLayer::ResetReading(); if (fpHTF) { const char* pszLine; while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (strcmp(pszLine, "SOUNDING DATA") == 0) { if (bHasFPK) pszLine = CPLReadLine2L(fpHTF, 1024, NULL); break; } } if (pszLine == NULL) bEOF = TRUE; } }
OGRFeature *OGRAeronavFAANAVAIDLayer::GetNextRawFeature() { char szBuffer[134]; while( true ) { const char* pszLine = CPLReadLine2L(fpAeronavFAA, 134, nullptr); if (pszLine == nullptr) { bEOF = true; return nullptr; } if (strlen(pszLine) != 132) continue; if ( !(pszLine[psRecordDesc->nLatStartCol-1] == 'N' || pszLine[psRecordDesc->nLatStartCol-1] == 'S') ) continue; if ( !(pszLine[psRecordDesc->nLonStartCol-1] == 'E' || pszLine[psRecordDesc->nLonStartCol-1] == 'W') ) continue; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID ++); for( int i=0; i < psRecordDesc->nFields; i++ ) { int nWidth = psRecordDesc->pasFields[i].nLastCol - psRecordDesc->pasFields[i].nStartCol + 1; strncpy(szBuffer, pszLine + psRecordDesc->pasFields[i].nStartCol - 1, nWidth); szBuffer[nWidth] = 0; while(nWidth > 0 && szBuffer[nWidth - 1] == ' ') { szBuffer[nWidth - 1] = 0; nWidth --; } if (nWidth != 0) poFeature->SetField(i, szBuffer); } double dfLat = 0.0; double dfLon = 0.0; GetLatLon(pszLine + psRecordDesc->nLatStartCol - 1, pszLine + psRecordDesc->nLonStartCol - 1, dfLat, dfLon); OGRGeometry* poGeom = new OGRPoint(dfLon, dfLat); poGeom->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly( poGeom ); return poFeature; } }
void OGRHTFPolygonLayer::ResetReading() { OGRHTFLayer::ResetReading(); if (fpHTF) { const char* pszLine; while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (strcmp(pszLine, "POLYGON DATA") == 0) { break; } } if (pszLine == NULL) bEOF = TRUE; } }
void OGRSEGP1Layer::ResetReading() { nNextFID = 0; bEOF = FALSE; VSIFSeekL( fp, 0, SEEK_SET ); /* Skip first 20 header lines */ const char* pszLine = NULL; for(int i=0; i<20;i++) { pszLine = CPLReadLine2L(fp,81,NULL); if (pszLine == NULL) { bEOF = TRUE; break; } } }
void OGRSEGP1Layer::ResetReading() { nNextFID = 0; bEOF = false; VSIFSeekL( fp, 0, SEEK_SET ); /* Skip first 20 header lines */ const char* pszLine = nullptr; for(int i=0; i<20;i++) { pszLine = CPLReadLine2L(fp,81,nullptr); if (pszLine == nullptr) { bEOF = true; break; } } }
GDALDataset *E00GRIDDataset::Open( GDALOpenInfo * poOpenInfo ) { if (!Identify(poOpenInfo)) return NULL; /* -------------------------------------------------------------------- */ /* Find dataset characteristics */ /* -------------------------------------------------------------------- */ VSILFILE* fp = VSIFOpenL(poOpenInfo->pszFilename, "rb"); if (fp == NULL) return NULL; if (poOpenInfo->eAccess == GA_Update) { CPLError( CE_Failure, CPLE_NotSupported, "The E00GRID driver does not support update access to existing" " datasets.\n" ); VSIFCloseL(fp); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ E00GRIDDataset *poDS = new E00GRIDDataset(); if (strstr((const char*)poOpenInfo->pabyHeader, "\r\n") != NULL) poDS->nBytesEOL = 2; poDS->fp = fp; /* read EXP 0 or EXP 1 line */ const char* pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL) { CPLDebug("E00GRID", "Bad 1st line"); delete poDS; return NULL; } bool bCompressed = STARTS_WITH_CI(pszLine, "EXP 1"); E00ReadPtr e00ReadPtr = NULL; if (bCompressed) { VSIRewindL(fp); e00ReadPtr = E00ReadCallbackOpen(poDS, E00GRIDDataset::ReadNextLine, E00GRIDDataset::Rewind); if (e00ReadPtr == NULL) { delete poDS; return NULL; } E00ReadNextLine(e00ReadPtr); poDS->e00ReadPtr = e00ReadPtr; } /* skip GRD 2 line */ if (e00ReadPtr) pszLine = E00ReadNextLine(e00ReadPtr); else pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL || !STARTS_WITH_CI(pszLine, "GRD 2")) { CPLDebug("E00GRID", "Bad 2nd line"); delete poDS; return NULL; } /* read ncols, nrows and nodata value */ if (e00ReadPtr) pszLine = E00ReadNextLine(e00ReadPtr); else pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL || strlen(pszLine) < E00_INT_SIZE+E00_INT_SIZE+2+E00_DOUBLE_SIZE) { CPLDebug("E00GRID", "Bad 3rd line"); delete poDS; return NULL; } const int nRasterXSize = atoi(pszLine); const int nRasterYSize = atoi(pszLine + E00_INT_SIZE); if (!GDALCheckDatasetDimensions(nRasterXSize, nRasterYSize)) { delete poDS; return NULL; } GDALDataType eDT = GDT_Float32; if (STARTS_WITH_CI(pszLine + E00_INT_SIZE + E00_INT_SIZE, " 1")) eDT = GDT_Int32; else if (STARTS_WITH_CI(pszLine + E00_INT_SIZE + E00_INT_SIZE, " 2")) eDT = GDT_Float32; else { CPLDebug("E00GRID", "Unknown data type : %s", pszLine); } const double dfNoData = CPLAtof(pszLine + E00_INT_SIZE + E00_INT_SIZE + 2); /* read pixel size */ if (e00ReadPtr) pszLine = E00ReadNextLine(e00ReadPtr); else pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL || strlen(pszLine) < 2*E00_DOUBLE_SIZE) { CPLDebug("E00GRID", "Bad 4th line"); delete poDS; return NULL; } /* double dfPixelX = CPLAtof(pszLine); double dfPixelY = CPLAtof(pszLine + E00_DOUBLE_SIZE); */ /* read xmin, ymin */ if (e00ReadPtr) pszLine = E00ReadNextLine(e00ReadPtr); else pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL || strlen(pszLine) < 2*E00_DOUBLE_SIZE) { CPLDebug("E00GRID", "Bad 5th line"); delete poDS; return NULL; } const double dfMinX = CPLAtof(pszLine); const double dfMinY = CPLAtof(pszLine + E00_DOUBLE_SIZE); /* read xmax, ymax */ if (e00ReadPtr) pszLine = E00ReadNextLine(e00ReadPtr); else pszLine = CPLReadLine2L(fp, 81, NULL); if (pszLine == NULL || strlen(pszLine) < 2*E00_DOUBLE_SIZE) { CPLDebug("E00GRID", "Bad 6th line"); delete poDS; return NULL; } const double dfMaxX = CPLAtof(pszLine); const double dfMaxY = CPLAtof(pszLine + E00_DOUBLE_SIZE); poDS->nRasterXSize = nRasterXSize; poDS->nRasterYSize = nRasterYSize; poDS->dfNoData = dfNoData; poDS->adfGeoTransform[0] = dfMinX; poDS->adfGeoTransform[1] = (dfMaxX - dfMinX) / nRasterXSize; poDS->adfGeoTransform[2] = 0; poDS->adfGeoTransform[3] = dfMaxY; poDS->adfGeoTransform[4] = 0; poDS->adfGeoTransform[5] = - (dfMaxY - dfMinY) / nRasterYSize; poDS->nDataStart = VSIFTellL(fp); if (bCompressed) { poDS->panOffsets = (vsi_l_offset*) VSIMalloc2(sizeof(vsi_l_offset), nRasterYSize); if (poDS->panOffsets == NULL) { delete poDS; return NULL; } } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->nBands = 1; for( int i = 0; i < poDS->nBands; i++ ) poDS->SetBand( i+1, new E00GRIDRasterBand( poDS, i+1, eDT ) ); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Support overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return poDS; }
const char* E00GRIDDataset::ReadNextLine(void * ptr) { E00GRIDDataset* poDS = (E00GRIDDataset*) ptr; poDS->nPosBeforeReadLine = VSIFTellL(poDS->fp); return CPLReadLine2L(poDS->fp, 256, NULL); }
OGRFeature *OGRSEGP1Layer::GetNextRawFeature() { if( bEOF ) return nullptr; const char* pszLine = nullptr; while( true ) { pszLine = CPLReadLine2L(fp,81,nullptr); if (pszLine == nullptr || STARTS_WITH_CI(pszLine, "EOF")) { bEOF = true; return nullptr; } int nLineLen = static_cast<int>(strlen(pszLine)); while(nLineLen > 0 && pszLine[nLineLen-1] == ' ') { ((char*)pszLine)[nLineLen-1] = '\0'; nLineLen --; } char* pszExpandedLine = ExpandTabs(pszLine); pszLine = pszExpandedLine; nLineLen = static_cast<int>(strlen(pszLine)); OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID ++); OGRGeometry* poGeom = nullptr; if (nLatitudeCol-1 + 19 <= nLineLen) { char szDeg[3+1]; char szMin[2+1]; char szSec[4+1]; ExtractField(szDeg, pszLine, nLatitudeCol-1, 2); ExtractField(szMin, pszLine, nLatitudeCol+2-1, 2); ExtractField(szSec, pszLine, nLatitudeCol+2+2-1, 4); double dfLat = atoi(szDeg) + atoi(szMin) / 60.0 + atoi(szSec) / 100.0 / 3600.0; if (pszLine[nLatitudeCol+2+2+4-1] == 'S') dfLat = -dfLat; poFeature->SetField(SEGP1_FIELD_LATITUDE, dfLat); ExtractField(szDeg, pszLine, nLatitudeCol+9-1, 3); ExtractField(szMin, pszLine, nLatitudeCol+9+3-1, 2); ExtractField(szSec, pszLine, nLatitudeCol+9+3+2-1, 4); double dfLon = atoi(szDeg) + atoi(szMin) / 60.0 + atoi(szSec) / 100.0 / 3600.0; if (pszLine[nLatitudeCol+9+3+2+4-1] == 'W') dfLon = -dfLon; poFeature->SetField(SEGP1_FIELD_LONGITUDE, dfLon); if (!bUseEastingNorthingAsGeometry) poGeom = new OGRPoint(dfLon, dfLat); } /* Normal layout -> extract other fields */ if (nLatitudeCol == 27 && nLineLen >= 26-1+1) { char szLineName[16 + 1]; ExtractField(szLineName, pszLine, 2-1, 16); int i = 15; while (i >= 0) { if (szLineName[i] == ' ') szLineName[i] = '\0'; else break; i --; } poFeature->SetField(SEGP1_FIELD_LINENAME, szLineName); char szPointNumber[8+1]; ExtractField(szPointNumber, pszLine, 18-1, 8); poFeature->SetField(SEGP1_FIELD_POINTNUMBER, atoi(szPointNumber)); char szReshootCode[1+1]; ExtractField(szReshootCode, pszLine, 26-1, 1); poFeature->SetField(SEGP1_FIELD_RESHOOTCODE, szReshootCode); if (nLineLen >= 61) { char szEasting[8+1]; ExtractField(szEasting, pszLine, 46-1, 8); double dfEasting = CPLAtof(szEasting); poFeature->SetField(SEGP1_FIELD_EASTING, dfEasting); char szNorthing[8+1]; ExtractField(szNorthing, pszLine, 54-1, 8); double dfNorthing = CPLAtof(szNorthing); poFeature->SetField(SEGP1_FIELD_NORTHING, dfNorthing); if (bUseEastingNorthingAsGeometry) poGeom = new OGRPoint(dfEasting, dfNorthing); } if (nLineLen >= 66) { char szDepth[5+1]; ExtractField(szDepth, pszLine, 62-1, 5); double dfDepth = CPLAtof(szDepth); poFeature->SetField(SEGP1_FIELD_DEPTH, dfDepth); } } if (poGeom) { if (poSRS) poGeom->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poGeom); } CPLFree(pszExpandedLine); return poFeature; } }
int OGRARCGENDataSource::Open( const char * pszFilename, int bUpdateIn) { if (bUpdateIn) { return FALSE; } pszName = CPLStrdup( pszFilename ); // -------------------------------------------------------------------- // Does this appear to be a Arc/Info generate file? // -------------------------------------------------------------------- VSILFILE* fp = VSIFOpenL(pszFilename, "rb"); if (fp == NULL) return FALSE; /* Check that the first line is compatible with a generate file */ /* and in particular contain >= 32 && <= 127 bytes */ const char* pszLine; CPLPushErrorHandler(CPLQuietErrorHandler); pszLine = CPLReadLine2L(fp,256,NULL); CPLPopErrorHandler(); if (pszLine == NULL) { VSIFCloseL(fp); return FALSE; } const char* szPtr = pszLine; for(;*szPtr != '\0';szPtr++) { if (*szPtr < 32) { VSIFCloseL(fp); return FALSE; } } char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); CSLDestroy(papszTokens); if (nTokens != 1 && nTokens != 3 && nTokens != 4) { VSIFCloseL(fp); return FALSE; } /* Go to end of file, and count the number of END keywords */ /* If there's 1, it's a point layer */ /* If there's 2, it's a linestring or polygon layer */ VSIFSeekL( fp, 0, SEEK_END ); vsi_l_offset nSize = VSIFTellL(fp); if (nSize < 10) { VSIFCloseL(fp); return FALSE; } char szBuffer[10+1]; VSIFSeekL( fp, nSize - 10, SEEK_SET ); VSIFReadL( szBuffer, 1, 10, fp ); szBuffer[10] = '\0'; VSIFSeekL( fp, 0, SEEK_SET ); OGRwkbGeometryType eType; szPtr = szBuffer; const char* szEnd = strstr(szPtr, "END"); if (szEnd == NULL) szEnd = strstr(szPtr, "end"); if (szEnd == NULL) { VSIFCloseL(fp); return FALSE; } szPtr = szEnd + 3; szEnd = strstr(szPtr, "END"); if (szEnd == NULL) szEnd = strstr(szPtr, "end"); if (szEnd == NULL) { pszLine = CPLReadLine2L(fp,256,NULL); if (pszLine == NULL) { VSIFCloseL(fp); return FALSE; } char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); CSLDestroy(papszTokens); if (nTokens == 3) eType = wkbPoint; else if (nTokens == 4) eType = wkbPoint25D; else { VSIFCloseL(fp); return FALSE; } } else { int nLineNumber = 0; eType = wkbUnknown; CPLString osFirstX, osFirstY; CPLString osLastX, osLastY; int bIs3D = FALSE; while((pszLine = CPLReadLine2L(fp,256,NULL)) != NULL) { nLineNumber ++; if (nLineNumber == 2) { char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); if (nTokens == 2 || nTokens == 3) { if (nTokens == 3) bIs3D = TRUE; osFirstX = papszTokens[0]; osFirstY = papszTokens[1]; } CSLDestroy(papszTokens); if (nTokens != 2 && nTokens != 3) break; } else if (nLineNumber > 2) { if (EQUAL(pszLine, "END")) { if (osFirstX.compare(osLastX) == 0 && osFirstY.compare(osLastY) == 0) eType = (bIs3D) ? wkbPolygon25D : wkbPolygon; else eType = (bIs3D) ? wkbLineString25D : wkbLineString; break; } char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); if (nTokens == 2 || nTokens == 3) { osLastX = papszTokens[0]; osLastY = papszTokens[1]; } CSLDestroy(papszTokens); if (nTokens != 2 && nTokens != 3) break; } } if (eType == wkbUnknown) { VSIFCloseL(fp); return FALSE; } } VSIFSeekL( fp, 0, SEEK_SET ); nLayers = 1; papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*)); papoLayers[0] = new OGRARCGENLayer(pszName, fp, eType); return TRUE; }
OGRHTFSoundingLayer::OGRHTFSoundingLayer( const char* pszFilename, int nZone, int bIsNorth, int nTotalSoundings ) : OGRHTFLayer(pszFilename, nZone, bIsNorth) { poFeatureDefn = new OGRFeatureDefn( "sounding" ); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType( wkbPoint ); this->nTotalSoundings = nTotalSoundings; bHasFPK = FALSE; nFieldsPresent = 0; panFieldPresence = NULL; nEastingIndex = -1; nNorthingIndex = -1; const char* pszLine; int bSoundingHeader = FALSE; while( fpHTF != NULL && (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (strncmp(pszLine, "SOUNDING HEADER", strlen("SOUNDING HEADER")) == 0) bSoundingHeader = TRUE; else if (bSoundingHeader && strlen(pszLine) > 10 && pszLine[0] == '[' && pszLine[3] == ']' && pszLine[4] == ' ' && strstr(pszLine + 5, " =") != NULL) { char* pszName = CPLStrdup(pszLine + 5); *strstr(pszName, " =") = 0; char* pszPtr = pszName; for(;*pszPtr;pszPtr++) { if (*pszPtr == ' ') *pszPtr = '_'; } OGRFieldType eType; if (strcmp(pszName, "REJECTED_SOUNDING") == 0 || strcmp(pszName, "FIX_NUMBER") == 0 || strcmp(pszName, "NBA_FLAG") == 0 || strcmp(pszName, "SOUND_VELOCITY") == 0 || strcmp(pszName, "PLOTTED_SOUNDING") == 0) eType = OFTInteger; else if (strcmp(pszName, "LATITUDE") == 0 || strcmp(pszName, "LONGITUDE") == 0 || strcmp(pszName, "EASTING") == 0 || strcmp(pszName, "NORTHING") == 0 || strcmp(pszName, "DEPTH") == 0 || strcmp(pszName, "TPE_POSITION") == 0 || strcmp(pszName, "TPE_DEPTH") == 0 || strcmp(pszName, "TIDE") == 0 || strcmp(pszName, "DEEP_WATER_CORRECTION") == 0 || strcmp(pszName, "VERTICAL_BIAS_CORRECTION") == 0) eType = OFTReal; else eType = OFTString; OGRFieldDefn oField( pszName, eType); poFeatureDefn->AddFieldDefn( &oField); CPLFree(pszName); } else if (strcmp(pszLine, "END OF SOUNDING HEADER") == 0) { bSoundingHeader = FALSE; } else if (strcmp(pszLine, "SOUNDING DATA") == 0) { pszLine = CPLReadLine2L(fpHTF, 1024, NULL); if (pszLine == NULL) break; if (pszLine[0] == '[' && (int)strlen(pszLine) == 2 + poFeatureDefn->GetFieldCount()) { bHasFPK = TRUE; panFieldPresence = (int*)CPLMalloc(sizeof(int) * poFeatureDefn->GetFieldCount()); int i; for(i=0;i<poFeatureDefn->GetFieldCount();i++) { panFieldPresence[i] = pszLine[1 + i] != '0'; nFieldsPresent += panFieldPresence[i]; } } break; } } if (!bHasFPK) { panFieldPresence = (int*)CPLMalloc(sizeof(int) * poFeatureDefn->GetFieldCount()); int i; for(i=0;i<poFeatureDefn->GetFieldCount();i++) panFieldPresence[i] = TRUE; nFieldsPresent = poFeatureDefn->GetFieldCount(); } int nIndex; nIndex = poFeatureDefn->GetFieldIndex("EASTING"); if (nIndex < 0 || !panFieldPresence[nIndex]) { CPLError(CE_Failure, CPLE_NotSupported, "Cannot find EASTING field"); VSIFCloseL( fpHTF ); fpHTF = NULL; return; } nEastingIndex = nIndex; nIndex = poFeatureDefn->GetFieldIndex("NORTHING"); if (nIndex < 0 || !panFieldPresence[nIndex]) { CPLError(CE_Failure, CPLE_NotSupported, "Cannot find NORTHING field"); VSIFCloseL( fpHTF ); fpHTF = NULL; return; } nNorthingIndex = nIndex; ResetReading(); }
OGRFeature *OGRHTFSoundingLayer::GetNextRawFeature() { const char* pszLine; OGRLinearRing oLR; while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (pszLine[0] == ';') { /* comment */ ; } else if (pszLine[0] == 0) { bEOF = TRUE; return NULL; } else if (strcmp(pszLine, "END OF SOUNDING DATA") == 0) { bEOF = TRUE; return NULL; } else break; } if (pszLine == NULL) { bEOF = TRUE; return NULL; } int i; double dfEasting = 0, dfNorthing = 0; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); char* pszStr = (char*)pszLine; for(i=0;i<poFeatureDefn->GetFieldCount();i++) { if (!panFieldPresence[i]) continue; char* pszSpace = strchr(pszStr, ' '); if (pszSpace) *pszSpace = '\0'; if (strcmp(pszStr, "*") != 0) poFeature->SetField(i, pszStr); if (i == nEastingIndex) dfEasting = poFeature->GetFieldAsDouble(i); else if (i == nNorthingIndex) dfNorthing = poFeature->GetFieldAsDouble(i); if (pszSpace == NULL) break; pszStr = pszSpace + 1; } OGRPoint* poPoint = new OGRPoint(dfEasting, dfNorthing); poPoint->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoint); poFeature->SetFID(nNextFID++); return poFeature; }
CPLErr XYZRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, void * pImage ) { XYZDataset *poGDS = (XYZDataset *) poDS; if (poGDS->fp == NULL) return CE_Failure; int nLineInFile = nBlockYOff * nBlockXSize; if (poGDS->nDataLineNum > nLineInFile) { poGDS->nDataLineNum = 0; VSIFSeekL(poGDS->fp, 0, SEEK_SET); for(int i=0;i<poGDS->nCommentLineCount;i++) CPLReadLine2L(poGDS->fp, 100, NULL); if (poGDS->bHasHeaderLine) { const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) { memset(pImage, 0, nBlockXSize * (GDALGetDataTypeSize(eDataType) / 8)); return CE_Failure; } poGDS->nLineNum ++; } } while(poGDS->nDataLineNum < nLineInFile) { const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) { memset(pImage, 0, nBlockXSize * (GDALGetDataTypeSize(eDataType) / 8)); return CE_Failure; } poGDS->nLineNum ++; const char* pszPtr = pszLine; char ch; int nCol = 0; int bLastWasSep = TRUE; while((ch = *pszPtr) != '\0') { if (ch == ' ' || ch == ',' || ch == '\t' || ch == ';') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else { bLastWasSep = FALSE; } pszPtr ++; } /* Skip empty line */ if (nCol == 0 && bLastWasSep) continue; poGDS->nDataLineNum ++; } int i; for(i=0;i<nBlockXSize;i++) { int nCol; int bLastWasSep; do { const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) { memset(pImage, 0, nBlockXSize * (GDALGetDataTypeSize(eDataType) / 8)); return CE_Failure; } poGDS->nLineNum ++; const char* pszPtr = pszLine; char ch; nCol = 0; bLastWasSep = TRUE; while((ch = *pszPtr) != '\0') { if (ch == ' ' || ch == ',' || ch == '\t' || ch == ';') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else { if (bLastWasSep && nCol == poGDS->nZIndex) { double dfZ = CPLAtofM(pszPtr); if (eDataType == GDT_Float32) { ((float*)pImage)[i] = (float)dfZ; } else if (eDataType == GDT_Int32) { ((GInt32*)pImage)[i] = (GInt32)dfZ; } else if (eDataType == GDT_Int16) { ((GInt16*)pImage)[i] = (GInt16)dfZ; } else { ((GByte*)pImage)[i] = (GByte)dfZ; } } bLastWasSep = FALSE; } pszPtr ++; } /* Skip empty line */ } while (nCol == 0 && bLastWasSep); poGDS->nDataLineNum ++; nCol ++; if (nCol < poGDS->nMinTokens) { memset(pImage, 0, nBlockXSize * (GDALGetDataTypeSize(eDataType) / 8)); return CE_Failure; } } CPLAssert(poGDS->nDataLineNum == (nBlockYOff + 1) * nBlockXSize); return CE_None; }
void OGRUKOOAP190Layer::ParseHeaders() { while(TRUE) { const char* pszLine = CPLReadLine2L(fp,81,NULL); if (pszLine == NULL || EQUALN(pszLine, "EOF", 3)) { break; } int nLineLen = strlen(pszLine); while(nLineLen > 0 && pszLine[nLineLen-1] == ' ') { ((char*)pszLine)[nLineLen-1] = '\0'; nLineLen --; } if (pszLine[0] != 'H') break; if (nLineLen < 33) continue; if (!bUseEastingNorthingAsGeometry && strncmp(pszLine, "H1500", 5) == 0 && poSRS == NULL) { if (strncmp(pszLine + 33 - 1, "WGS84", 5) == 0 || strncmp(pszLine + 33 - 1, "WGS-84", 6) == 0) { poSRS = new OGRSpatialReference(SRS_WKT_WGS84); } else if (strncmp(pszLine + 33 - 1, "WGS72", 5) == 0) { poSRS = new OGRSpatialReference(); poSRS->SetFromUserInput("WGS72"); } } else if (!bUseEastingNorthingAsGeometry && strncmp(pszLine, "H1501", 5) == 0 && poSRS != NULL && nLineLen >= 32 + 6 * 6 + 10) { char aszParams[6][6+1]; char szZ[10+1]; int i; for(i=0;i<6;i++) { ExtractField(aszParams[i], pszLine, 33 - 1 + i * 6, 6); } ExtractField(szZ, pszLine, 33 - 1 + 6 * 6, 10); poSRS->SetTOWGS84(CPLAtof(aszParams[0]), CPLAtof(aszParams[1]), CPLAtof(aszParams[2]), CPLAtof(aszParams[3]), CPLAtof(aszParams[4]), CPLAtof(aszParams[5]), CPLAtof(szZ)); } else if (strncmp(pszLine, "H0200", 5) == 0) { char** papszTokens = CSLTokenizeString(pszLine + 33 - 1); for(int i = 0; papszTokens[i] != NULL; i++) { if (strlen(papszTokens[i]) == 4) { int nVal = atoi(papszTokens[i]); if (nVal >= 1900) { if (nYear != 0 && nYear != nVal) { CPLDebug("SEGUKOOA", "Several years found in H0200. Ignoring them!"); nYear = 0; break; } nYear = nVal; } } } CSLDestroy(papszTokens); } } VSIFSeekL( fp, 0, SEEK_SET ); }
OGRFeature *OGRARCGENLayer::GetNextRawFeature() { if (bEOF) return NULL; const char* pszLine; OGRwkbGeometryType eType = poFeatureDefn->GetGeomType(); if (wkbFlatten(eType) == wkbPoint) { while(TRUE) { pszLine = CPLReadLine2L(fp,256,NULL); if (pszLine == NULL || EQUAL(pszLine, "END")) { bEOF = TRUE; return NULL; } char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); if (nTokens == 3 || nTokens == 4) { OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID ++); poFeature->SetField(0, papszTokens[0]); if (nTokens == 3) poFeature->SetGeometryDirectly( new OGRPoint(CPLAtof(papszTokens[1]), CPLAtof(papszTokens[2]))); else poFeature->SetGeometryDirectly( new OGRPoint(CPLAtof(papszTokens[1]), CPLAtof(papszTokens[2]), CPLAtof(papszTokens[3]))); CSLDestroy(papszTokens); return poFeature; } else CSLDestroy(papszTokens); } } CPLString osID; OGRLinearRing* poLR = (wkbFlatten(eType) == wkbPolygon) ? new OGRLinearRing() : NULL; OGRLineString* poLS = (wkbFlatten(eType) == wkbLineString) ? new OGRLineString() : poLR; while(TRUE) { pszLine = CPLReadLine2L(fp,256,NULL); if (pszLine == NULL) break; if (EQUAL(pszLine, "END")) { if (osID.size() == 0) break; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID ++); poFeature->SetField(0, osID.c_str()); if (wkbFlatten(eType) == wkbPolygon) { OGRPolygon* poPoly = new OGRPolygon(); poPoly->addRingDirectly(poLR); poFeature->SetGeometryDirectly(poPoly); } else poFeature->SetGeometryDirectly(poLS); return poFeature; } char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 ); int nTokens = CSLCount(papszTokens); if (osID.size() == 0) { if (nTokens >= 1) osID = papszTokens[0]; else { CSLDestroy(papszTokens); break; } } else { if (nTokens == 2) { poLS->addPoint(CPLAtof(papszTokens[0]), CPLAtof(papszTokens[1])); } else if (nTokens == 3) { poLS->addPoint(CPLAtof(papszTokens[0]), CPLAtof(papszTokens[1]), CPLAtof(papszTokens[2])); } else { CSLDestroy(papszTokens); break; } } CSLDestroy(papszTokens); } bEOF = TRUE; delete poLS; return NULL; }
GDALDataset *XYZDataset::Open( GDALOpenInfo * poOpenInfo ) { int i; int bHasHeaderLine; int nCommentLineCount = 0; if (!IdentifyEx(poOpenInfo, bHasHeaderLine, nCommentLineCount)) return NULL; CPLString osFilename(poOpenInfo->pszFilename); /* GZipped .xyz files are common, so automagically open them */ /* if the /vsigzip/ has not been explicitly passed */ if (strlen(poOpenInfo->pszFilename) > 6 && EQUAL(poOpenInfo->pszFilename + strlen(poOpenInfo->pszFilename) - 6, "xyz.gz") && !EQUALN(poOpenInfo->pszFilename, "/vsigzip/", 9)) { osFilename = "/vsigzip/"; osFilename += poOpenInfo->pszFilename; } /* -------------------------------------------------------------------- */ /* Find dataset characteristics */ /* -------------------------------------------------------------------- */ VSILFILE* fp = VSIFOpenL(osFilename.c_str(), "rb"); if (fp == NULL) return NULL; /* For better performance of CPLReadLine2L() we create a buffered reader */ /* (except for /vsigzip/ since it has one internally) */ if (!EQUALN(poOpenInfo->pszFilename, "/vsigzip/", 9)) fp = (VSILFILE*) VSICreateBufferedReaderHandle((VSIVirtualHandle*)fp); const char* pszLine; int nXIndex = -1, nYIndex = -1, nZIndex = -1; int nMinTokens = 0; for(i=0;i<nCommentLineCount;i++) CPLReadLine2L(fp, 100, NULL); /* -------------------------------------------------------------------- */ /* Parse header line */ /* -------------------------------------------------------------------- */ if (bHasHeaderLine) { pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } char** papszTokens = CSLTokenizeString2( pszLine, " ,\t;", CSLT_HONOURSTRINGS ); int nTokens = CSLCount(papszTokens); if (nTokens < 3) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, found %d tokens. Expected 3 at least", 1, nTokens); CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } int i; for(i=0;i<nTokens;i++) { if (EQUAL(papszTokens[i], "x") || EQUALN(papszTokens[i], "lon", 3) || EQUALN(papszTokens[i], "east", 4)) nXIndex = i; else if (EQUAL(papszTokens[i], "y") || EQUALN(papszTokens[i], "lat", 3) || EQUALN(papszTokens[i], "north", 5)) nYIndex = i; else if (EQUAL(papszTokens[i], "z") || EQUALN(papszTokens[i], "alt", 3) || EQUAL(papszTokens[i], "height")) nZIndex = i; } CSLDestroy(papszTokens); papszTokens = NULL; if (nXIndex < 0 || nYIndex < 0 || nZIndex < 0) { CPLError(CE_Warning, CPLE_AppDefined, "Could not find one of the X, Y or Z column names in header line. Defaulting to the first 3 columns"); nXIndex = 0; nYIndex = 1; nZIndex = 2; } nMinTokens = 1 + MAX(MAX(nXIndex, nYIndex), nZIndex); } else { nXIndex = 0; nYIndex = 1; nZIndex = 2; nMinTokens = 3; } /* -------------------------------------------------------------------- */ /* Parse data lines */ /* -------------------------------------------------------------------- */ int nLineNum = 0; int nDataLineNum = 0; double dfX = 0, dfY = 0, dfZ = 0; double dfMinX = 0, dfMinY = 0, dfMaxX = 0, dfMaxY = 0; double dfMinZ = 0, dfMaxZ = 0; double dfLastX = 0, dfLastY = 0; std::vector<double> adfStepX, adfStepY; GDALDataType eDT = GDT_Byte; int bSameNumberOfValuesPerLine = TRUE; char chDecimalSep = '\0'; int bStepYSign = 0; while((pszLine = CPLReadLine2L(fp, 100, NULL)) != NULL) { nLineNum ++; const char* pszPtr = pszLine; char ch; int nCol = 0; int bLastWasSep = TRUE; if( chDecimalSep == '\0' ) { int nCountComma = 0; int nCountFieldSep = 0; while((ch = *pszPtr) != '\0') { if( ch == '.' ) { chDecimalSep = '.'; break; } else if( ch == ',' ) { nCountComma ++; bLastWasSep = FALSE; } else if( ch == ' ' ) { if (!bLastWasSep) nCountFieldSep ++; bLastWasSep = TRUE; } else if( ch == '\t' || ch == ';' ) { nCountFieldSep ++; bLastWasSep = TRUE; } else bLastWasSep = FALSE; pszPtr ++; } if( chDecimalSep == '\0' ) { /* 1,2,3 */ if( nCountComma >= 2 && nCountFieldSep == 0 ) chDecimalSep = '.'; /* 23,5;33;45 */ else if ( nCountComma > 0 && nCountFieldSep > 0 ) chDecimalSep = ','; } pszPtr = pszLine; bLastWasSep = TRUE; } char chLocalDecimalSep = chDecimalSep ? chDecimalSep : '.'; while((ch = *pszPtr) != '\0') { if (ch == ' ') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else if ((ch == ',' && chLocalDecimalSep != ',') || ch == '\t' || ch == ';') { nCol ++; bLastWasSep = TRUE; } else { if (bLastWasSep) { if (nCol == nXIndex) dfX = CPLAtofDelim(pszPtr, chLocalDecimalSep); else if (nCol == nYIndex) dfY = CPLAtofDelim(pszPtr, chLocalDecimalSep); else if (nCol == nZIndex) { dfZ = CPLAtofDelim(pszPtr, chLocalDecimalSep); if( nDataLineNum == 0 ) dfMinZ = dfMaxZ = dfZ; else if( dfZ < dfMinZ ) dfMinZ = dfZ; else if( dfZ > dfMaxZ ) dfMaxZ = dfZ; int nZ = (int)dfZ; if ((double)nZ != dfZ) { eDT = GDT_Float32; } else if ((eDT == GDT_Byte || eDT == GDT_Int16) && (nZ < 0 || nZ > 255)) { if (nZ < -32768 || nZ > 32767) eDT = GDT_Int32; else eDT = GDT_Int16; } } } bLastWasSep = FALSE; } pszPtr ++; } /* skip empty lines */ if (bLastWasSep && nCol == 0) { continue; } nDataLineNum ++; nCol ++; if (nCol < nMinTokens) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, found %d tokens. Expected %d at least", nLineNum, nCol, nMinTokens); VSIFCloseL(fp); return NULL; } if (nDataLineNum == 1) { dfMinX = dfMaxX = dfX; dfMinY = dfMaxY = dfY; } else { double dfStepY = dfY - dfLastY; if( dfStepY == 0.0 ) { double dfStepX = dfX - dfLastX; if( dfStepX <= 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, X spacing was %f. Expected >0 value", nLineNum, dfStepX); VSIFCloseL(fp); return NULL; } if( std::find(adfStepX.begin(), adfStepX.end(), dfStepX) == adfStepX.end() ) { int bAddNewValue = TRUE; std::vector<double>::iterator oIter = adfStepX.begin(); while( oIter != adfStepX.end() ) { if( dfStepX < *oIter && fmod( *oIter, dfStepX ) < 1e-8 ) { adfStepX.erase(oIter); } else if( dfStepX > *oIter && fmod( dfStepX, *oIter ) < 1e-8 ) { bAddNewValue = FALSE; break; } else { ++ oIter; } } if( bAddNewValue ) { adfStepX.push_back(dfStepX); if( adfStepX.size() == 10 ) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: too many stepX values"); VSIFCloseL(fp); return NULL; } } } } else { int bNewStepYSign = (dfStepY < 0.0) ? -1 : 1; if( bStepYSign == 0 ) bStepYSign = bNewStepYSign; else if( bStepYSign != bNewStepYSign ) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, change of Y direction", nLineNum); VSIFCloseL(fp); return NULL; } if( bNewStepYSign < 0 ) dfStepY = -dfStepY; if( adfStepY.size() == 0 ) adfStepY.push_back(dfStepY); else if( adfStepY[0] != dfStepY ) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, too many stepY values", nLineNum); VSIFCloseL(fp); return NULL; } } if (dfX < dfMinX) dfMinX = dfX; if (dfX > dfMaxX) dfMaxX = dfX; if (dfY < dfMinY) dfMinY = dfY; if (dfY > dfMaxY) dfMaxY = dfY; } dfLastX = dfX; dfLastY = dfY; } if (adfStepX.size() != 1) { CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine X spacing"); VSIFCloseL(fp); return NULL; } if (adfStepY.size() != 1) { CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine Y spacing"); VSIFCloseL(fp); return NULL; } double dfStepX = adfStepX[0]; double dfStepY = adfStepY[0] * bStepYSign; int nXSize = 1 + int((dfMaxX - dfMinX) / dfStepX + 0.5); int nYSize = 1 + int((dfMaxY - dfMinY) / fabs(dfStepY) + 0.5); //CPLDebug("XYZ", "minx=%f maxx=%f stepx=%f", dfMinX, dfMaxX, dfStepX); //CPLDebug("XYZ", "miny=%f maxy=%f stepy=%f", dfMinY, dfMaxY, dfStepY); if (nDataLineNum != nXSize * nYSize) { bSameNumberOfValuesPerLine = FALSE; } if (poOpenInfo->eAccess == GA_Update) { CPLError( CE_Failure, CPLE_NotSupported, "The XYZ driver does not support update access to existing" " datasets.\n" ); VSIFCloseL(fp); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ XYZDataset *poDS; poDS = new XYZDataset(); poDS->fp = fp; poDS->bHasHeaderLine = bHasHeaderLine; poDS->nCommentLineCount = nCommentLineCount; poDS->chDecimalSep = chDecimalSep ? chDecimalSep : '.'; poDS->nXIndex = nXIndex; poDS->nYIndex = nYIndex; poDS->nZIndex = nZIndex; poDS->nMinTokens = nMinTokens; poDS->nRasterXSize = nXSize; poDS->nRasterYSize = nYSize; poDS->adfGeoTransform[0] = dfMinX - dfStepX / 2; poDS->adfGeoTransform[1] = dfStepX; poDS->adfGeoTransform[3] = (dfStepY < 0) ? dfMaxY - dfStepY / 2 : dfMinY - dfStepY / 2; poDS->adfGeoTransform[5] = dfStepY; poDS->bSameNumberOfValuesPerLine = bSameNumberOfValuesPerLine; poDS->dfMinZ = dfMinZ; poDS->dfMaxZ = dfMaxZ; //CPLDebug("XYZ", "bSameNumberOfValuesPerLine = %d", bSameNumberOfValuesPerLine); if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize)) { delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->nBands = 1; for( i = 0; i < poDS->nBands; i++ ) poDS->SetBand( i+1, new XYZRasterBand( poDS, i+1, eDT ) ); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Support overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }
CPLErr XYZRasterBand::IReadBlock( CPL_UNUSED int nBlockXOff, int nBlockYOff, void * pImage ) { XYZDataset *poGDS = (XYZDataset *) poDS; if (poGDS->fp == NULL) return CE_Failure; if( pImage ) { int bSuccess = FALSE; double dfNoDataValue = GetNoDataValue(&bSuccess); if( !bSuccess ) dfNoDataValue = 0.0; GDALCopyWords(&dfNoDataValue, GDT_Float64, 0, pImage, eDataType, GDALGetDataTypeSize(eDataType) / 8, nRasterXSize); } int nLineInFile = nBlockYOff * nBlockXSize; // only valid if bSameNumberOfValuesPerLine if ( (poGDS->bSameNumberOfValuesPerLine && poGDS->nDataLineNum > nLineInFile) || (!poGDS->bSameNumberOfValuesPerLine && (nLastYOff == -1 || nBlockYOff == 0)) ) { poGDS->nDataLineNum = 0; poGDS->nLineNum = 0; VSIFSeekL(poGDS->fp, 0, SEEK_SET); for(int i=0;i<poGDS->nCommentLineCount;i++) { CPLReadLine2L(poGDS->fp, 100, NULL); poGDS->nLineNum ++; } if (poGDS->bHasHeaderLine) { const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) return CE_Failure; poGDS->nLineNum ++; } } if( !poGDS->bSameNumberOfValuesPerLine && nBlockYOff != nLastYOff + 1 ) { int iY; if( nBlockYOff < nLastYOff ) { nLastYOff = -1; for(iY = 0; iY < nBlockYOff; iY++) { if( IReadBlock(0, iY, NULL) != CE_None ) return CE_Failure; } } else { for(iY = nLastYOff + 1; iY < nBlockYOff; iY++) { if( IReadBlock(0, iY, NULL) != CE_None ) return CE_Failure; } } } else if( poGDS->bSameNumberOfValuesPerLine ) { while(poGDS->nDataLineNum < nLineInFile) { const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) return CE_Failure; poGDS->nLineNum ++; const char* pszPtr = pszLine; char ch; int nCol = 0; int bLastWasSep = TRUE; while((ch = *pszPtr) != '\0') { if (ch == ' ') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else if ((ch == ',' && poGDS->chDecimalSep != ',') || ch == '\t' || ch == ';') { nCol ++; bLastWasSep = TRUE; } else { bLastWasSep = FALSE; } pszPtr ++; } /* Skip empty line */ if (nCol == 0 && bLastWasSep) continue; poGDS->nDataLineNum ++; } } double dfExpectedY = poGDS->adfGeoTransform[3] + (0.5 + nBlockYOff) * poGDS->adfGeoTransform[5]; int idx = -1; while(TRUE) { int nCol; int bLastWasSep; do { vsi_l_offset nOffsetBefore = VSIFTellL(poGDS->fp); const char* pszLine = CPLReadLine2L(poGDS->fp, 100, 0); if (pszLine == NULL) { if( poGDS->bSameNumberOfValuesPerLine ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot read line %d", poGDS->nLineNum + 1); return CE_Failure; } else { nLastYOff = nBlockYOff; return CE_None; } } poGDS->nLineNum ++; const char* pszPtr = pszLine; char ch; nCol = 0; bLastWasSep = TRUE; double dfX = 0.0, dfY = 0.0, dfZ = 0.0; int bUsefulColsFound = 0; while((ch = *pszPtr) != '\0') { if (ch == ' ') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else if ((ch == ',' && poGDS->chDecimalSep != ',') || ch == '\t' || ch == ';') { nCol ++; bLastWasSep = TRUE; } else { if (bLastWasSep) { if (nCol == poGDS->nXIndex) { bUsefulColsFound ++; if( !poGDS->bSameNumberOfValuesPerLine ) dfX = CPLAtofDelim(pszPtr, poGDS->chDecimalSep); } else if (nCol == poGDS->nYIndex) { bUsefulColsFound ++; if( !poGDS->bSameNumberOfValuesPerLine ) dfY = CPLAtofDelim(pszPtr, poGDS->chDecimalSep); } else if( nCol == poGDS->nZIndex) { bUsefulColsFound ++; dfZ = CPLAtofDelim(pszPtr, poGDS->chDecimalSep); } } bLastWasSep = FALSE; } pszPtr ++; } nCol ++; if( bUsefulColsFound == 3 ) { if( poGDS->bSameNumberOfValuesPerLine ) { idx ++; } else { if( fabs(dfY - dfExpectedY) > 1e-8 ) { if( idx < 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, found %f instead of %f for nBlockYOff = %d", poGDS->nLineNum, dfY, dfExpectedY, nBlockYOff); return CE_Failure; } VSIFSeekL(poGDS->fp, nOffsetBefore, SEEK_SET); nLastYOff = nBlockYOff; poGDS->nLineNum --; return CE_None; } idx = (int)((dfX - 0.5 * poGDS->adfGeoTransform[1] - poGDS->adfGeoTransform[0]) / poGDS->adfGeoTransform[1] + 0.5); } CPLAssert(idx >= 0 && idx < nRasterXSize); if( pImage ) { if (eDataType == GDT_Float32) { ((float*)pImage)[idx] = (float)dfZ; } else if (eDataType == GDT_Int32) { ((GInt32*)pImage)[idx] = (GInt32)dfZ; } else if (eDataType == GDT_Int16) { ((GInt16*)pImage)[idx] = (GInt16)dfZ; } else { ((GByte*)pImage)[idx] = (GByte)dfZ; } } } /* Skip empty line */ } while (nCol == 1 && bLastWasSep); poGDS->nDataLineNum ++; if (nCol < poGDS->nMinTokens) return CE_Failure; if( idx + 1 == nRasterXSize ) break; } if( poGDS->bSameNumberOfValuesPerLine ) { CPLAssert(poGDS->nDataLineNum == (nBlockYOff + 1) * nBlockXSize); } nLastYOff = nBlockYOff; return CE_None; }
int OGRPDSDataSource::Open( const char * pszFilename ) { pszName = CPLStrdup( pszFilename ); // -------------------------------------------------------------------- // Does this appear to be a .PDS table file? // -------------------------------------------------------------------- VSILFILE* fp = VSIFOpenL(pszFilename, "rb"); if (fp == NULL) return FALSE; char szBuffer[512]; int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp); szBuffer[nbRead] = '\0'; const char* pszPos = strstr(szBuffer, "PDS_VERSION_ID"); int bIsPDS = (pszPos != NULL); if (!bIsPDS) { VSIFCloseL(fp); return FALSE; } if (!oKeywords.Ingest(fp, pszPos - szBuffer)) { VSIFCloseL(fp); return FALSE; } VSIFCloseL(fp); CPLString osRecordType = oKeywords.GetKeyword( "RECORD_TYPE", "" ); CPLString osFileRecords = oKeywords.GetKeyword( "FILE_RECORDS", "" ); CPLString osRecordBytes = oKeywords.GetKeyword( "RECORD_BYTES", "" ); int nRecordSize = atoi(osRecordBytes); if (osRecordType.size() == 0 || osFileRecords.size() == 0 || osRecordBytes.size() == 0 || nRecordSize <= 0) { CPLError(CE_Failure, CPLE_NotSupported, "One of RECORD_TYPE, FILE_RECORDS or RECORD_BYTES is missing"); return FALSE; } CleanString(osRecordType); if (osRecordType.compare("FIXED_LENGTH") != 0) { CPLError(CE_Failure, CPLE_NotSupported, "Only RECORD_TYPE=FIXED_LENGTH is supported"); return FALSE; } CPLString osTable = oKeywords.GetKeyword( "^TABLE", "" ); if (osTable.size() != 0) LoadTable(pszFilename, nRecordSize, "TABLE"); else { VSILFILE* fp = VSIFOpenL(pszFilename, "rb"); if (fp == NULL) return FALSE; while(TRUE) { CPLPushErrorHandler(CPLQuietErrorHandler); const char* pszLine = CPLReadLine2L(fp, 256, NULL); CPLPopErrorHandler(); CPLErrorReset(); if (pszLine == NULL) break; char** papszTokens = CSLTokenizeString2( pszLine, " =", CSLT_HONOURSTRINGS ); int nTokens = CSLCount(papszTokens); if (nTokens == 2 && papszTokens[0][0] == '^' && strstr(papszTokens[0], "TABLE") != NULL) { LoadTable(pszFilename, nRecordSize, papszTokens[0] + 1); } CSLDestroy(papszTokens); papszTokens = NULL; } VSIFCloseL(fp); } return nLayers != 0; }
GDALDataset *SNODASDataset::Open( GDALOpenInfo * poOpenInfo ) { if( !Identify(poOpenInfo) ) return NULL; VSILFILE *fp; fp = VSIFOpenL( poOpenInfo->pszFilename, "r" ); if( fp == NULL ) { return NULL; } const char * pszLine; int nRows = -1, nCols = -1; CPLString osDataFilename; int bIsInteger = FALSE, bIs2Bytes = FALSE; double dfNoData = 0; int bHasNoData = FALSE; double dfMin = 0; int bHasMin = FALSE; double dfMax = 0; int bHasMax = FALSE; double dfMinX = 0.0, dfMinY = 0.0, dfMaxX = 0.0, dfMaxY = 0.0; int bHasMinX = FALSE, bHasMinY = FALSE, bHasMaxX = FALSE, bHasMaxY = FALSE; int bNotProjected = FALSE, bIsWGS84 = FALSE; CPLString osDescription, osDataUnits; int nStartYear = -1, nStartMonth = -1, nStartDay = -1, nStartHour = -1, nStartMinute = -1, nStartSecond = -1; int nStopYear = -1, nStopMonth = -1, nStopDay = -1, nStopHour = -1, nStopMinute = -1, nStopSecond = -1; while( (pszLine = CPLReadLine2L( fp, 256, NULL )) != NULL ) { char** papszTokens = CSLTokenizeStringComplex( pszLine, ":", TRUE, FALSE ); if( CSLCount( papszTokens ) != 2 ) { CSLDestroy( papszTokens ); continue; } if( papszTokens[1][0] == ' ' ) memmove(papszTokens[1], papszTokens[1] + 1, strlen(papszTokens[1] + 1) + 1); if( EQUAL(papszTokens[0],"Data file pathname") ) { osDataFilename = papszTokens[1]; } else if( EQUAL(papszTokens[0],"Description") ) { osDescription = papszTokens[1]; } else if( EQUAL(papszTokens[0],"Data units") ) { osDataUnits= papszTokens[1]; } else if( EQUAL(papszTokens[0],"Start year") ) nStartYear = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Start month") ) nStartMonth = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Start day") ) nStartDay = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Start hour") ) nStartHour = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Start minute") ) nStartMinute = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Start second") ) nStartSecond = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop year") ) nStopYear = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop month") ) nStopMonth = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop day") ) nStopDay = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop hour") ) nStopHour = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop minute") ) nStopMinute = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Stop second") ) nStopSecond = atoi(papszTokens[1]); else if( EQUAL(papszTokens[0],"Number of columns") ) { nCols = atoi(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Number of rows") ) { nRows = atoi(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Data type")) { bIsInteger = EQUAL(papszTokens[1],"integer"); } else if( EQUAL(papszTokens[0],"Data bytes per pixel")) { bIs2Bytes = EQUAL(papszTokens[1],"2"); } else if( EQUAL(papszTokens[0],"Projected")) { bNotProjected = EQUAL(papszTokens[1],"no"); } else if( EQUAL(papszTokens[0],"Horizontal datum")) { bIsWGS84 = EQUAL(papszTokens[1],"WGS84"); } else if( EQUAL(papszTokens[0],"No data value")) { bHasNoData = TRUE; dfNoData = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Minimum data value")) { bHasMin = TRUE; dfMin = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Maximum data value")) { bHasMax = TRUE; dfMax = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Minimum x-axis coordinate") ) { bHasMinX = TRUE; dfMinX = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Minimum y-axis coordinate") ) { bHasMinY = TRUE; dfMinY = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Maximum x-axis coordinate") ) { bHasMaxX = TRUE; dfMaxX = CPLAtofM(papszTokens[1]); } else if( EQUAL(papszTokens[0],"Maximum y-axis coordinate") ) { bHasMaxY = TRUE; dfMaxY = CPLAtofM(papszTokens[1]); } CSLDestroy( papszTokens ); } VSIFCloseL( fp ); /* -------------------------------------------------------------------- */ /* Did we get the required keywords? If not we return with */ /* this never having been considered to be a match. This isn't */ /* an error! */ /* -------------------------------------------------------------------- */ if( nRows == -1 || nCols == -1 || !bIsInteger || !bIs2Bytes ) return NULL; if( !bNotProjected || !bIsWGS84 ) return NULL; if( osDataFilename.size() == 0 ) return NULL; if (!GDALCheckDatasetDimensions(nCols, nRows)) return NULL; /* -------------------------------------------------------------------- */ /* Open target binary file. */ /* -------------------------------------------------------------------- */ const char* pszPath = CPLGetPath(poOpenInfo->pszFilename); osDataFilename = CPLFormFilename(pszPath, osDataFilename, NULL); VSILFILE* fpRaw = VSIFOpenL( osDataFilename, "rb" ); if( fpRaw == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ SNODASDataset *poDS; poDS = new SNODASDataset(); poDS->nRasterXSize = nCols; poDS->nRasterYSize = nRows; poDS->osDataFilename = osDataFilename; poDS->bHasNoData = bHasNoData; poDS->dfNoData = dfNoData; poDS->bHasMin = bHasMin; poDS->dfMin = dfMin; poDS->bHasMax = bHasMax; poDS->dfMax = dfMax; if (bHasMinX && bHasMinY && bHasMaxX && bHasMaxY) { poDS->bGotTransform = TRUE; poDS->adfGeoTransform[0] = dfMinX; poDS->adfGeoTransform[1] = (dfMaxX - dfMinX) / nCols; poDS->adfGeoTransform[2] = 0.0; poDS->adfGeoTransform[3] = dfMaxY; poDS->adfGeoTransform[4] = 0.0; poDS->adfGeoTransform[5] = - (dfMaxY - dfMinY) / nRows; } if (osDescription.size()) poDS->SetMetadataItem("Description", osDescription); if (osDataUnits.size()) poDS->SetMetadataItem("Data_Units", osDataUnits); if (nStartYear != -1 && nStartMonth != -1 && nStartDay != -1 && nStartHour != -1 && nStartMinute != -1 && nStartSecond != -1) poDS->SetMetadataItem("Start_Date", CPLSPrintf("%04d/%02d/%02d %02d:%02d:%02d", nStartYear, nStartMonth, nStartDay, nStartHour, nStartMinute, nStartSecond)); if (nStopYear != -1 && nStopMonth != -1 && nStopDay != -1 && nStopHour != -1 && nStopMinute != -1 && nStopSecond != -1) poDS->SetMetadataItem("Stop_Date", CPLSPrintf("%04d/%02d/%02d %02d:%02d:%02d", nStopYear, nStopMonth, nStopDay, nStopHour, nStopMinute, nStopSecond)); /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->SetBand( 1, new SNODASRasterBand( fpRaw, nCols, nRows) ); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }
int OGRSEGUKOOADataSource::Open( const char * pszFilename ) { pszName = CPLStrdup( pszFilename ); VSILFILE* fp = VSIFOpenL(pszFilename, "rb"); if (fp == nullptr) return FALSE; CPLPushErrorHandler(CPLQuietErrorHandler); const char* pszLine = CPLReadLine2L(fp,81,nullptr); CPLPopErrorHandler(); CPLErrorReset(); /* Both UKOOA P1/90 and SEG-P1 begins by a H character */ if (pszLine == nullptr || pszLine[0] != 'H') { VSIFCloseL(fp); return FALSE; } // -------------------------------------------------------------------- // Does this appear to be a UKOOA P1/90 file? // -------------------------------------------------------------------- if (STARTS_WITH(pszLine, "H0100 ")) { VSIFSeekL( fp, 0, SEEK_SET ); VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb"); if (fp2 == nullptr) { VSIFCloseL(fp); return FALSE; } nLayers = 2; papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*)); papoLayers[0] = new OGRUKOOAP190Layer(pszName, fp); papoLayers[1] = new OGRSEGUKOOALineLayer(pszName, new OGRUKOOAP190Layer(pszName, fp2)); return TRUE; } // -------------------------------------------------------------------- // Does this appear to be a SEG-P1 file? // -------------------------------------------------------------------- /* Check first 20 header lines, and fetch the first point */ for(int iLine = 0; iLine < 21; iLine ++) { const char* szPtr = pszLine; for(;*szPtr != '\0';szPtr++) { if (*szPtr != 9 && *szPtr < 32) { VSIFCloseL(fp); return FALSE; } } if (iLine == 20) break; CPLPushErrorHandler(CPLQuietErrorHandler); pszLine = CPLReadLine2L(fp,81,nullptr); CPLPopErrorHandler(); CPLErrorReset(); if (pszLine == nullptr) { VSIFCloseL(fp); return FALSE; } } char* pszExpandedLine = OGRSEGP1Layer::ExpandTabs(pszLine); int nLatitudeCol = OGRSEGP1Layer::DetectLatitudeColumn(pszExpandedLine); CPLFree(pszExpandedLine); if (nLatitudeCol > 0) { VSIFSeekL( fp, 0, SEEK_SET ); VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb"); if (fp2 == nullptr) { VSIFCloseL(fp); return FALSE; } nLayers = 2; papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*)); papoLayers[0] = new OGRSEGP1Layer(pszName, fp, nLatitudeCol); papoLayers[1] = new OGRSEGUKOOALineLayer(pszName, new OGRSEGP1Layer(pszName, fp2, nLatitudeCol)); return TRUE; } VSIFCloseL(fp); return FALSE; }
GDALDataset *XYZDataset::Open( GDALOpenInfo * poOpenInfo ) { int i; int bHasHeaderLine; int nCommentLineCount = 0; if (!IdentifyEx(poOpenInfo, bHasHeaderLine, nCommentLineCount)) return NULL; CPLString osFilename(poOpenInfo->pszFilename); /* GZipped .xyz files are common, so automagically open them */ /* if the /vsigzip/ has not been explicitely passed */ if (strlen(poOpenInfo->pszFilename) > 6 && EQUAL(poOpenInfo->pszFilename + strlen(poOpenInfo->pszFilename) - 6, "xyz.gz") && !EQUALN(poOpenInfo->pszFilename, "/vsigzip/", 9)) { osFilename = "/vsigzip/"; osFilename += poOpenInfo->pszFilename; } /* -------------------------------------------------------------------- */ /* Find dataset characteristics */ /* -------------------------------------------------------------------- */ VSILFILE* fp = VSIFOpenL(osFilename.c_str(), "rb"); if (fp == NULL) return NULL; /* For better performance of CPLReadLine2L() we create a buffered reader */ /* (except for /vsigzip/ since it has one internally) */ if (!EQUALN(poOpenInfo->pszFilename, "/vsigzip/", 9)) fp = (VSILFILE*) VSICreateBufferedReaderHandle((VSIVirtualHandle*)fp); const char* pszLine; int nXIndex = -1, nYIndex = -1, nZIndex = -1; int nMinTokens = 0; for(i=0;i<nCommentLineCount;i++) CPLReadLine2L(fp, 100, NULL); /* -------------------------------------------------------------------- */ /* Parse header line */ /* -------------------------------------------------------------------- */ if (bHasHeaderLine) { pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } char** papszTokens = CSLTokenizeString2( pszLine, " ,\t;", CSLT_HONOURSTRINGS ); int nTokens = CSLCount(papszTokens); if (nTokens < 3) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, found %d tokens. Expected 3 at least", 1, nTokens); CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } int i; for(i=0;i<nTokens;i++) { if (EQUAL(papszTokens[i], "x") || EQUALN(papszTokens[i], "lon", 3) || EQUALN(papszTokens[i], "east", 4)) nXIndex = i; else if (EQUAL(papszTokens[i], "y") || EQUALN(papszTokens[i], "lat", 3) || EQUALN(papszTokens[i], "north", 5)) nYIndex = i; else if (EQUAL(papszTokens[i], "z") || EQUALN(papszTokens[i], "alt", 3) || EQUAL(papszTokens[i], "height")) nZIndex = i; } CSLDestroy(papszTokens); papszTokens = NULL; if (nXIndex < 0 || nYIndex < 0 || nZIndex < 0) { CPLError(CE_Warning, CPLE_AppDefined, "Could not find one of the X, Y or Z column names in header line. Defaulting to the first 3 columns"); nXIndex = 0; nYIndex = 1; nZIndex = 2; } nMinTokens = 1 + MAX(MAX(nXIndex, nYIndex), nZIndex); } else { nXIndex = 0; nYIndex = 1; nZIndex = 2; nMinTokens = 3; } /* -------------------------------------------------------------------- */ /* Parse data lines */ /* -------------------------------------------------------------------- */ int nXSize = 0, nYSize = 0; int nLineNum = 0; int nDataLineNum = 0; double dfFirstX = 0; double dfX = 0, dfY = 0, dfZ = 0; double dfMinX = 0, dfMinY = 0, dfMaxX = 0, dfMaxY = 0; double dfLastX = 0, dfLastY = 0; double dfStepX = 0, dfStepY = 0; GDALDataType eDT = GDT_Byte; while((pszLine = CPLReadLine2L(fp, 100, NULL)) != NULL) { nLineNum ++; const char* pszPtr = pszLine; char ch; int nCol = 0; int bLastWasSep = TRUE; while((ch = *pszPtr) != '\0') { if (ch == ' ' || ch == ',' || ch == '\t' || ch == ';') { if (!bLastWasSep) nCol ++; bLastWasSep = TRUE; } else { if (bLastWasSep) { if (nCol == nXIndex) dfX = CPLAtofM(pszPtr); else if (nCol == nYIndex) dfY = CPLAtofM(pszPtr); else if (nCol == nZIndex && eDT != GDT_Float32) { dfZ = CPLAtofM(pszPtr); int nZ = (int)dfZ; if ((double)nZ != dfZ) { eDT = GDT_Float32; } else if ((eDT == GDT_Byte || eDT == GDT_Int16) && (nZ < 0 || nZ > 255)) { if (nZ < -32768 || nZ > 32767) eDT = GDT_Int32; else eDT = GDT_Int16; } } } bLastWasSep = FALSE; } pszPtr ++; } /* skip empty lines */ if (bLastWasSep && nCol == 0) { continue; } nDataLineNum ++; nCol ++; if (nCol < nMinTokens) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, found %d tokens. Expected %d at least", nLineNum, nCol, nMinTokens); VSIFCloseL(fp); return NULL; } if (nDataLineNum == 1) { dfFirstX = dfMinX = dfMaxX = dfX; dfMinY = dfMaxY = dfY; } else { if (dfX < dfMinX) dfMinX = dfX; if (dfX > dfMaxX) dfMaxX = dfX; if (dfY < dfMinY) dfMinY = dfY; if (dfY > dfMaxY) dfMaxY = dfY; } if (nDataLineNum == 2) { dfStepX = dfX - dfLastX; if (dfStepX <= 0) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, X spacing was %f. Expected >0 value", nLineNum, dfStepX); VSIFCloseL(fp); return NULL; } } else if (nDataLineNum > 2) { double dfNewStepX = dfX - dfLastX; double dfNewStepY = dfY - dfLastY; if (dfNewStepY != 0) { nYSize ++; if (dfStepY == 0) { nXSize = nDataLineNum - 1; double dfAdjustedStepX = (dfMaxX - dfMinX) / (nXSize - 1); if (fabs(dfStepX - dfAdjustedStepX) > 1e-8) { CPLDebug("XYZ", "Adjusting stepx from %f to %f", dfStepX, dfAdjustedStepX); } dfStepX = dfAdjustedStepX; } if (dfStepY != 0 && fabs(dfX - dfFirstX) > 1e-8) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, X is %f, where as %f was expected", nLineNum, dfX, dfFirstX); VSIFCloseL(fp); return NULL; } if (dfStepY != 0 && fabs(dfLastX - dfMaxX) > 1e-8) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, X is %f, where as %f was expected", nLineNum - 1, dfLastX, dfMaxX); VSIFCloseL(fp); return NULL; } /*if (dfStepY != 0 && fabs(dfNewStepY - dfStepY) > 1e-8) { CPLError(CE_Failure, CPLE_AppDefined, "Ungridded dataset: At line %d, Y spacing was %f, whereas it was %f before", nLineNum, dfNewStepY, dfStepY); VSIFCloseL(fp); return NULL; }*/ dfStepY = dfNewStepY; } else if (dfNewStepX != 0) { /*if (dfStepX != 0 && fabs(dfNewStepX - dfStepX) > 1e-8) { CPLError(CE_Failure, CPLE_AppDefined, "At line %d, X spacing was %f, whereas it was %f before", nLineNum, dfNewStepX, dfStepX); VSIFCloseL(fp); return NULL; }*/ } } dfLastX = dfX; dfLastY = dfY; } nYSize ++; if (dfStepX == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine X spacing"); VSIFCloseL(fp); return NULL; } if (dfStepY == 0) { CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine Y spacing"); VSIFCloseL(fp); return NULL; } double dfAdjustedStepY = ((dfStepY < 0) ? -1 : 1) * (dfMaxY - dfMinY) / (nYSize - 1); if (fabs(dfStepY - dfAdjustedStepY) > 1e-8) { CPLDebug("XYZ", "Adjusting stepy from %f to %f", dfStepY, dfAdjustedStepY); } dfStepY = dfAdjustedStepY; //CPLDebug("XYZ", "minx=%f maxx=%f stepx=%f", dfMinX, dfMaxX, dfStepX); //CPLDebug("XYZ", "miny=%f maxy=%f stepy=%f", dfMinY, dfMaxY, dfStepY); if (nDataLineNum != nXSize * nYSize) { CPLError(CE_Failure, CPLE_AppDefined, "Found %d lines. Expected %d", nDataLineNum,nXSize * nYSize); VSIFCloseL(fp); return NULL; } if (poOpenInfo->eAccess == GA_Update) { CPLError( CE_Failure, CPLE_NotSupported, "The XYZ driver does not support update access to existing" " datasets.\n" ); VSIFCloseL(fp); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ XYZDataset *poDS; poDS = new XYZDataset(); poDS->fp = fp; poDS->bHasHeaderLine = bHasHeaderLine; poDS->nCommentLineCount = nCommentLineCount; poDS->nXIndex = nXIndex; poDS->nYIndex = nYIndex; poDS->nZIndex = nZIndex; poDS->nMinTokens = nMinTokens; poDS->nRasterXSize = nXSize; poDS->nRasterYSize = nYSize; poDS->adfGeoTransform[0] = dfMinX - dfStepX / 2; poDS->adfGeoTransform[1] = dfStepX; poDS->adfGeoTransform[3] = (dfStepY < 0) ? dfMaxY - dfStepY / 2 : dfMinY - dfStepY / 2; poDS->adfGeoTransform[5] = dfStepY; if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize)) { delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->nBands = 1; for( i = 0; i < poDS->nBands; i++ ) poDS->SetBand( i+1, new XYZRasterBand( poDS, i+1, eDT ) ); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Support overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }
OGRFeature *OGRSUALayer::GetNextRawFeature() { if( bEOF ) return nullptr; CPLString osTYPE; CPLString osCLASS; CPLString osTITLE; CPLString osTOPS; CPLString osBASE; OGRLinearRing oLR; double dfLastLat = 0.0; double dfLastLon = 0.0; bool bFirst = true; while( true ) { const char* pszLine = nullptr; if( bFirst && bHasLastLine ) { pszLine = osLastLine.c_str(); bFirst = false; } else { pszLine = CPLReadLine2L(fpSUA, 1024, nullptr); if (pszLine == nullptr) { bEOF = true; if (oLR.getNumPoints() == 0) return nullptr; break; } osLastLine = pszLine; bHasLastLine = true; } if (pszLine[0] == '#' || pszLine[0] == '\0') continue; if (STARTS_WITH_CI(pszLine, "TYPE=")) { if (!osTYPE.empty()) break; osTYPE = pszLine + 5; } else if (STARTS_WITH_CI(pszLine, "CLASS=")) { if (!osCLASS.empty()) break; osCLASS = pszLine + 6; } else if (STARTS_WITH_CI(pszLine, "TITLE=")) { if (!osTITLE.empty()) break; osTITLE = pszLine + 6; } else if (STARTS_WITH_CI(pszLine, "TOPS=")) osTOPS = pszLine + 5; else if (STARTS_WITH_CI(pszLine, "BASE=")) osBASE = pszLine + 5; else if (STARTS_WITH_CI(pszLine, "POINT=")) { pszLine += 6; if (strlen(pszLine) != 16) continue; double dfLat = 0.0; double dfLon = 0.0; if (!GetLatLon(pszLine, dfLat, dfLon)) continue; oLR.addPoint(dfLon, dfLat); dfLastLat = dfLat; dfLastLon = dfLon; } else if (STARTS_WITH_CI(pszLine, "CLOCKWISE") || STARTS_WITH_CI(pszLine, "ANTI-CLOCKWISE")) { if (oLR.getNumPoints() == 0) continue; int bClockWise = STARTS_WITH_CI(pszLine, "CLOCKWISE"); /*const char* pszRADIUS = strstr(pszLine, "RADIUS="); if (pszRADIUS == NULL) continue; double dfRADIUS = CPLAtof(pszRADIUS + 7) * 1852;*/ const char* pszCENTRE = strstr(pszLine, "CENTRE="); if (pszCENTRE == nullptr) continue; pszCENTRE += 7; if (strlen(pszCENTRE) < 17 || pszCENTRE[16] != ' ') continue; double dfCenterLat = 0.0; double dfCenterLon = 0.0; if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon)) continue; const char* pszTO = strstr(pszLine, "TO="); if (pszTO == nullptr) continue; pszTO += 3; if (strlen(pszTO) != 16) continue; double dfToLat = 0.0; double dfToLon = 0.0; if (!GetLatLon(pszTO, dfToLat, dfToLon)) continue; const double dfStartDistance = OGR_GreatCircle_Distance(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon); const double dfEndDistance = OGR_GreatCircle_Distance(dfCenterLat, dfCenterLon, dfToLat, dfToLon); const double dfStartAngle = OGR_GreatCircle_InitialHeading(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon); double dfEndAngle = OGR_GreatCircle_InitialHeading(dfCenterLat, dfCenterLon, dfToLat, dfToLon); if( bClockWise && dfEndAngle < dfStartAngle ) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; int nSign = (bClockWise) ? 1 : -1; for( double dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign ) { const double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); const double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; double dfLat = 0.0; double dfLon = 0.0; OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } oLR.addPoint(dfToLon, dfToLat); dfLastLat = oLR.getY(oLR.getNumPoints() - 1); dfLastLon = oLR.getX(oLR.getNumPoints() - 1); } else if (STARTS_WITH_CI(pszLine, "CIRCLE")) { const char* pszRADIUS = strstr(pszLine, "RADIUS="); if (pszRADIUS == nullptr) continue; double dfRADIUS = CPLAtof(pszRADIUS + 7) * 1852; const char* pszCENTRE = strstr(pszLine, "CENTRE="); if (pszCENTRE == nullptr) continue; pszCENTRE += 7; if (strlen(pszCENTRE) != 16) continue; double dfCenterLat = 0.0; double dfCenterLon = 0.0; if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon)) continue; double dfLat = 0.0; double dfLon = 0.0; for( double dfAngle = 0; dfAngle < 360; dfAngle += 1 ) { OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, 0, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); dfLastLat = oLR.getY(oLR.getNumPoints() - 1); dfLastLon = oLR.getX(oLR.getNumPoints() - 1); } else if (STARTS_WITH_CI(pszLine, "INCLUDE") || STARTS_WITH_CI(pszLine, "END")) { } else { CPLDebug("SUA", "Unexpected content : %s", pszLine); } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetField(0, osTYPE.c_str()); poFeature->SetField(1, osCLASS.c_str()); poFeature->SetField(2, osTITLE.c_str()); poFeature->SetField(3, osTOPS.c_str()); poFeature->SetField(4, osBASE.c_str()); OGRPolygon* poPoly = new OGRPolygon(); poPoly->assignSpatialReference(poSRS); oLR.closeRings(); poPoly->addRing(&oLR); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
OGRFeature *OGRHTFPolygonLayer::GetNextRawFeature() { OGRFeature* poFeature = new OGRFeature(poFeatureDefn); const char* pszLine; OGRLinearRing oLR; int bHastFirstCoord = FALSE; double dfFirstEasting = 0, dfFirstNorthing = 0; double dfIslandEasting = 0, dfIslandNorthing = 0; int bInIsland = FALSE; OGRPolygon* poPoly = new OGRPolygon(); while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (pszLine[0] == ';') { /* comment */ ; } else if (pszLine[0] == 0) { /* end of polygon is marked by a blank line */ break; } else if (strncmp(pszLine, "POLYGON DESCRIPTION: ", strlen("POLYGON DESCRIPTION: ")) == 0) { poFeature->SetField(0, pszLine + strlen("POLYGON DESCRIPTION: ")); } else if (strncmp(pszLine, "POLYGON IDENTIFIER: ", strlen("POLYGON IDENTIFIER: ")) == 0) { poFeature->SetField(1, pszLine + strlen("POLYGON IDENTIFIER: ")); } else if (strncmp(pszLine, "SEAFLOOR COVERAGE: ", strlen("SEAFLOOR COVERAGE:")) == 0) { const char* pszVal = pszLine + strlen("SEAFLOOR COVERAGE: "); if (*pszVal != '*') poFeature->SetField(2, pszVal); } else if (strncmp(pszLine, "POSITION ACCURACY: ", strlen("POSITION ACCURACY:")) == 0) { const char* pszVal = pszLine + strlen("POSITION ACCURACY: "); if (*pszVal != '*') poFeature->SetField(3, pszVal); } else if (strncmp(pszLine, "DEPTH ACCURACY: ", strlen("DEPTH ACCURACY:")) == 0) { const char* pszVal = pszLine + strlen("DEPTH ACCURACY: "); if (*pszVal != '*') poFeature->SetField(4, pszVal); } else if (strcmp(pszLine, "END OF POLYGON DATA") == 0) { bEOF = TRUE; break; } else { char** papszTokens = CSLTokenizeString(pszLine); if (CSLCount(papszTokens) == 4) { double dfEasting = atof(papszTokens[2]); double dfNorthing = atof(papszTokens[3]); if (!bHastFirstCoord) { bHastFirstCoord = TRUE; dfFirstEasting = dfEasting; dfFirstNorthing = dfNorthing; oLR.addPoint(dfEasting, dfNorthing); } else if (dfFirstEasting == dfEasting && dfFirstNorthing == dfNorthing) { if (!bInIsland) { oLR.addPoint(dfEasting, dfNorthing); poPoly->addRing(&oLR); oLR.empty(); bInIsland = TRUE; } } else if (bInIsland && oLR.getNumPoints() == 0) { dfIslandEasting = dfEasting; dfIslandNorthing = dfNorthing; oLR.addPoint(dfEasting, dfNorthing); } else if (bInIsland && dfIslandEasting == dfEasting && dfIslandNorthing == dfNorthing) { oLR.addPoint(dfEasting, dfNorthing); poPoly->addRing(&oLR); oLR.empty(); } else { oLR.addPoint(dfEasting, dfNorthing); } } CSLDestroy(papszTokens); } } if (pszLine == NULL) bEOF = TRUE; if (oLR.getNumPoints() >= 3) { oLR.closeRings(); poPoly->addRing(&oLR); } poPoly->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
void OGRUKOOAP190Layer::ParseHeaders() { while( true ) { const char* pszLine = CPLReadLine2L(fp,81,nullptr); if (pszLine == nullptr || STARTS_WITH_CI(pszLine, "EOF")) { break; } int nLineLen = static_cast<int>(strlen(pszLine)); while(nLineLen > 0 && pszLine[nLineLen-1] == ' ') { ((char*)pszLine)[nLineLen-1] = '\0'; nLineLen --; } if (pszLine[0] != 'H') break; if (nLineLen < 33) continue; if (!bUseEastingNorthingAsGeometry && STARTS_WITH(pszLine, "H1500") && poSRS == nullptr) { if (STARTS_WITH(pszLine + 33 - 1, "WGS84") || STARTS_WITH(pszLine + 33 - 1, "WGS-84")) { poSRS = new OGRSpatialReference(SRS_WKT_WGS84_LAT_LONG); poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); } else if (STARTS_WITH(pszLine + 33 - 1, "WGS72")) { poSRS = new OGRSpatialReference(); poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); poSRS->SetFromUserInput("WGS72"); } } else if (!bUseEastingNorthingAsGeometry && STARTS_WITH(pszLine, "H1501") && poSRS != nullptr && nLineLen >= 32 + 6 * 6 + 10) { char aszParams[6][6+1]; char szZ[10+1]; for( int i = 0; i < 6; i++ ) { ExtractField(aszParams[i], pszLine, 33 - 1 + i * 6, 6); } ExtractField(szZ, pszLine, 33 - 1 + 6 * 6, 10); poSRS->SetTOWGS84(CPLAtof(aszParams[0]), CPLAtof(aszParams[1]), CPLAtof(aszParams[2]), CPLAtof(aszParams[3]), CPLAtof(aszParams[4]), CPLAtof(aszParams[5]), CPLAtof(szZ)); } else if (STARTS_WITH(pszLine, "H0200")) { char** papszTokens = CSLTokenizeString(pszLine + 33 - 1); for(int i = 0; papszTokens[i] != nullptr; i++) { if (strlen(papszTokens[i]) == 4) { int nVal = atoi(papszTokens[i]); if (nVal >= 1900) { if( nYear != 0 && nYear != nVal ) { CPLDebug("SEGUKOOA", "Several years found in H0200. Ignoring them!"); nYear = 0; break; } nYear = nVal; } } } CSLDestroy(papszTokens); } } VSIFSeekL( fp, 0, SEEK_SET ); }
GDALDataset *ZMapDataset::Open( GDALOpenInfo * poOpenInfo ) { if (!Identify(poOpenInfo)) return NULL; /* -------------------------------------------------------------------- */ /* Find dataset characteristics */ /* -------------------------------------------------------------------- */ VSILFILE* fp = VSIFOpenL(poOpenInfo->pszFilename, "rb"); if (fp == NULL) return NULL; const char* pszLine; while((pszLine = CPLReadLine2L(fp, 100, NULL)) != NULL) { if (*pszLine == '!') { continue; } else break; } if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } /* Parse first header line */ char** papszTokens = CSLTokenizeString2( pszLine, ",", 0 ); if (CSLCount(papszTokens) != 3) { CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } int nValuesPerLine = atoi(papszTokens[2]); if (nValuesPerLine <= 0) { CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } CSLDestroy(papszTokens); papszTokens = NULL; /* Parse second header line */ pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } papszTokens = CSLTokenizeString2( pszLine, ",", 0 ); if (CSLCount(papszTokens) != 5) { CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } int nFieldSize = atoi(papszTokens[0]); double dfNoDataValue = CPLAtofM(papszTokens[1]); int nDecimalCount = atoi(papszTokens[3]); int nColumnNumber = atoi(papszTokens[4]); CSLDestroy(papszTokens); papszTokens = NULL; if (nFieldSize <= 0 || nFieldSize >= 40 || nDecimalCount <= 0 || nDecimalCount >= nFieldSize || nColumnNumber != 1) { CPLDebug("ZMap", "nFieldSize=%d, nDecimalCount=%d, nColumnNumber=%d", nFieldSize, nDecimalCount, nColumnNumber); VSIFCloseL(fp); return NULL; } /* Parse third header line */ pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } papszTokens = CSLTokenizeString2( pszLine, ",", 0 ); if (CSLCount(papszTokens) != 6) { CSLDestroy(papszTokens); VSIFCloseL(fp); return NULL; } int nRows = atoi(papszTokens[0]); int nCols = atoi(papszTokens[1]); double dfMinX = CPLAtofM(papszTokens[2]); double dfMaxX = CPLAtofM(papszTokens[3]); double dfMinY = CPLAtofM(papszTokens[4]); double dfMaxY = CPLAtofM(papszTokens[5]); CSLDestroy(papszTokens); papszTokens = NULL; if (!GDALCheckDatasetDimensions(nCols, nRows) || nCols == 1 || nRows == 1) { VSIFCloseL(fp); return NULL; } /* Ignore fourth header line */ pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL) { VSIFCloseL(fp); return NULL; } /* Check fifth header line */ pszLine = CPLReadLine2L(fp, 100, NULL); if (pszLine == NULL || pszLine[0] != '@') { VSIFCloseL(fp); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ ZMapDataset *poDS; poDS = new ZMapDataset(); poDS->fp = fp; poDS->nDataStartOff = VSIFTellL(fp); poDS->nValuesPerLine = nValuesPerLine; poDS->nFieldSize = nFieldSize; poDS->nDecimalCount = nDecimalCount; poDS->nRasterXSize = nCols; poDS->nRasterYSize = nRows; poDS->dfNoDataValue = dfNoDataValue; if (CSLTestBoolean(CPLGetConfigOption("ZMAP_PIXEL_IS_POINT", "FALSE"))) { double dfStepX = (dfMaxX - dfMinX) / (nCols - 1); double dfStepY = (dfMaxY - dfMinY) / (nRows - 1); poDS->adfGeoTransform[0] = dfMinX - dfStepX / 2; poDS->adfGeoTransform[1] = dfStepX; poDS->adfGeoTransform[3] = dfMaxY + dfStepY / 2; poDS->adfGeoTransform[5] = -dfStepY; } else { double dfStepX = (dfMaxX - dfMinX) / nCols ; double dfStepY = (dfMaxY - dfMinY) / nRows; poDS->adfGeoTransform[0] = dfMinX; poDS->adfGeoTransform[1] = dfStepX; poDS->adfGeoTransform[3] = dfMaxY; poDS->adfGeoTransform[5] = -dfStepY; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->nBands = 1; poDS->SetBand( 1, new ZMapRasterBand( poDS ) ); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Support overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }
OGRFeature *OGRUKOOAP190Layer::GetNextRawFeature() { if( bEOF ) return nullptr; while( true ) { const char* pszLine = CPLReadLine2L(fp, 81, nullptr); if (pszLine == nullptr || STARTS_WITH_CI(pszLine, "EOF")) { bEOF = true; return nullptr; } int nLineLen = static_cast<int>(strlen(pszLine)); while(nLineLen > 0 && pszLine[nLineLen-1] == ' ') { ((char*)pszLine)[nLineLen-1] = '\0'; nLineLen --; } if (pszLine[0] == 'H' || nLineLen < 46) continue; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID ++); char szLineName[12 + 1]; ExtractField(szLineName, pszLine, 2-1, 12); int i = 11; while (i >= 0) { if (szLineName[i] == ' ') szLineName[i] = '\0'; else break; i --; } poFeature->SetField(FIELD_LINENAME, szLineName); char szVesselId[1+1]; szVesselId[0] = pszLine[17-1]; if (szVesselId[0] != ' ') { szVesselId[1] = '\0'; poFeature->SetField(FIELD_VESSEL_ID, szVesselId); } char szSourceId[1+1]; szSourceId[0] = pszLine[18-1]; if (szSourceId[0] != ' ') { szSourceId[1] = '\0'; poFeature->SetField(FIELD_SOURCE_ID, szSourceId); } char szOtherId[1+1]; szOtherId[0] = pszLine[19-1]; if (szOtherId[0] != ' ') { szOtherId[1] = '\0'; poFeature->SetField(FIELD_OTHER_ID, szOtherId); } char szPointNumber[6+1]; ExtractField(szPointNumber, pszLine, 20-1, 6); poFeature->SetField(4, atoi(szPointNumber)); char szDeg[3+1]; char szMin[2+1]; char szSec[5+1]; ExtractField(szDeg, pszLine, 26-1, 2); ExtractField(szMin, pszLine, 26+2-1, 2); ExtractField(szSec, pszLine, 26+2+2-1, 5); double dfLat = atoi(szDeg) + atoi(szMin) / 60.0 + CPLAtof(szSec) / 3600.0; if (pszLine[26+2+2+5-1] == 'S') dfLat = -dfLat; poFeature->SetField(FIELD_LATITUDE, dfLat); ExtractField(szDeg, pszLine, 36-1, 3); ExtractField(szMin, pszLine, 36+3-1, 2); ExtractField(szSec, pszLine, 36+3+2-1, 5); double dfLon = atoi(szDeg) + atoi(szMin) / 60.0 + CPLAtof(szSec) / 3600.0; if (pszLine[36+3+2+5-1] == 'W') dfLon = -dfLon; poFeature->SetField(FIELD_LONGITUDE, dfLon); OGRGeometry* poGeom = nullptr; if (!bUseEastingNorthingAsGeometry) poGeom = new OGRPoint(dfLon, dfLat); if (nLineLen >= 64) { char szEasting[9+1]; ExtractField(szEasting, pszLine, 47-1, 9); double dfEasting = CPLAtof(szEasting); poFeature->SetField(FIELD_EASTING, dfEasting); char szNorthing[9+1]; ExtractField(szNorthing, pszLine, 56-1, 9); double dfNorthing = CPLAtof(szNorthing); poFeature->SetField(FIELD_NORTHING, dfNorthing); if (bUseEastingNorthingAsGeometry) poGeom = new OGRPoint(dfEasting, dfNorthing); } if (poGeom) { if (poSRS) poGeom->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poGeom); } if (nLineLen >= 70) { char szDepth[6+1]; ExtractField(szDepth, pszLine, 65-1, 6); double dfDepth = CPLAtof(szDepth); poFeature->SetField(FIELD_DEPTH, dfDepth); } int nDayOfYear = 0; if (nLineLen >= 73) { char szDayOfYear[3+1]; ExtractField(szDayOfYear, pszLine, 71-1, 3); nDayOfYear = atoi(szDayOfYear); poFeature->SetField(FIELD_DAYOFYEAR, nDayOfYear); } if (nLineLen >= 79) { char szH[2+1], szM[2+1], szS[2+1]; ExtractField(szH, pszLine, 74-1, 2); ExtractField(szM, pszLine, 74-1+2, 2); ExtractField(szS, pszLine, 74-1+2+2, 2); poFeature->SetField(FIELD_TIME, 0, 0, 0, atoi(szH), atoi(szM), static_cast<float>(atoi(szS)) ); if( nYear != 0 ) { constexpr int mon_lengths[2][12] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} } ; const bool bIsLeap = isleap(nYear); int nMonth = 0; int nDays = 0; if ((bIsLeap && nDayOfYear >= 1 && nDayOfYear <= 366) || (!bIsLeap && nDayOfYear >= 1 && nDayOfYear <= 365)) { const int leap_offset = bIsLeap ? 1 : 0; while( nDayOfYear > nDays + mon_lengths[leap_offset][nMonth] ) { nDays += mon_lengths[leap_offset][nMonth]; nMonth ++; } const int nDayOfMonth = nDayOfYear - nDays; nMonth++; poFeature->SetField(FIELD_DATETIME, nYear, nMonth, nDayOfMonth, atoi(szH), atoi(szM), static_cast<float>(atoi(szS)) ); } } } return poFeature; } }
OGRFeature *OGROpenAirLayer::GetNextRawFeature() { const char* pszLine; CPLString osCLASS, osNAME, osFLOOR, osCEILING; OGRLinearRing oLR; /* double dfLastLat = 0, dfLastLon = 0; */ int bFirst = TRUE; int bClockWise = TRUE; double dfCenterLat = 0, dfCenterLon = 0; int bHasCenter = FALSE; OpenAirStyle sStyle; sStyle.penStyle = -1; sStyle.penWidth = -1; sStyle.penR = sStyle.penG = sStyle.penB = -1; sStyle.fillR = sStyle.fillG = sStyle.fillB = -1; if (bEOF) return NULL; while(TRUE) { if (bFirst && bHasLastLine) { pszLine = osLastLine.c_str(); bFirst = FALSE; } else { pszLine = CPLReadLine2L(fpOpenAir, 1024, NULL); if (pszLine == NULL) { bEOF = TRUE; if (oLR.getNumPoints() == 0) return NULL; if (osCLASS.size() != 0 && oStyleMap.find(osCLASS) != oStyleMap.end()) { memcpy(&sStyle, oStyleMap[osCLASS], sizeof(sStyle)); } break; } osLastLine = pszLine; bHasLastLine = TRUE; } if (pszLine[0] == '*' || pszLine[0] == '\0') continue; if (EQUALN(pszLine, "AC ", 3) || EQUALN(pszLine, "AC,", 3)) { if (osCLASS.size() != 0) { if (sStyle.penStyle != -1 || sStyle.fillR != -1) { if (oLR.getNumPoints() == 0) { OpenAirStyle* psStyle; if (oStyleMap.find(osCLASS) == oStyleMap.end()) { psStyle = (OpenAirStyle*)CPLMalloc( sizeof(OpenAirStyle)); oStyleMap[osCLASS] = psStyle; } else psStyle = oStyleMap[osCLASS]; memcpy(psStyle, &sStyle, sizeof(sStyle)); } else break; } else if (oStyleMap.find(osCLASS) != oStyleMap.end()) { memcpy(&sStyle, oStyleMap[osCLASS], sizeof(sStyle)); break; } else break; } sStyle.penStyle = -1; sStyle.penWidth = -1; sStyle.penR = sStyle.penG = sStyle.penB = -1; sStyle.fillR = sStyle.fillG = sStyle.fillB = -1; osCLASS = pszLine + 3; bClockWise = TRUE; bHasCenter = FALSE; } else if (EQUALN(pszLine, "AN ", 3)) { if (osNAME.size() != 0) break; osNAME = pszLine + 3; } else if (EQUALN(pszLine, "AH ", 3)) osCEILING = pszLine + 3; else if (EQUALN(pszLine, "AL ", 3)) osFLOOR = pszLine + 3; else if (EQUALN(pszLine, "AT ", 3)) { /* Ignored for that layer*/ } else if (EQUALN(pszLine, "SP ", 3)) { if (osCLASS.size() != 0) { char** papszTokens = CSLTokenizeString2(pszLine+3, ", ", 0); if (CSLCount(papszTokens) == 5) { sStyle.penStyle = atoi(papszTokens[0]); sStyle.penWidth = atoi(papszTokens[1]); sStyle.penR = atoi(papszTokens[2]); sStyle.penG = atoi(papszTokens[3]); sStyle.penB = atoi(papszTokens[4]); } CSLDestroy(papszTokens); } } else if (EQUALN(pszLine, "SB ", 3)) { if (osCLASS.size() != 0) { char** papszTokens = CSLTokenizeString2(pszLine+3, ", ", 0); if (CSLCount(papszTokens) == 3) { sStyle.fillR = atoi(papszTokens[0]); sStyle.fillG = atoi(papszTokens[1]); sStyle.fillB = atoi(papszTokens[2]); } CSLDestroy(papszTokens); } } else if (EQUALN(pszLine, "DP ", 3)) { pszLine += 3; double dfLat, dfLon; if (!OGROpenAirGetLatLon(pszLine, dfLat, dfLon)) continue; oLR.addPoint(dfLon, dfLat); /* dfLastLat = dfLat; */ /* dfLastLon = dfLon; */ } else if (EQUALN(pszLine, "DA ", 3)) { pszLine += 3; char* pszStar = strchr((char*)pszLine, '*'); if (pszStar) *pszStar = 0; char** papszTokens = CSLTokenizeString2(pszLine, ",", 0); if (bHasCenter && CSLCount(papszTokens) == 3) { double dfRadius = CPLAtof(papszTokens[0]) * 1852; double dfStartAngle = CPLAtof(papszTokens[1]); double dfEndAngle = CPLAtof(papszTokens[2]); if (bClockWise && dfEndAngle < dfStartAngle) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; double dfStartDistance = dfRadius; double dfEndDistance = dfRadius; int nSign = (bClockWise) ? 1 : -1; double dfAngle; double dfLat, dfLon; for(dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign) { double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfEndDistance, dfEndAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } CSLDestroy(papszTokens); } else if (EQUALN(pszLine, "DB ", 3)) { pszLine += 3; char* pszStar = strchr((char*)pszLine, '*'); if (pszStar) *pszStar = 0; char** papszTokens = CSLTokenizeString2(pszLine, ",", 0); double dfFirstLat, dfFirstLon; double dfSecondLat, dfSecondLon; if (bHasCenter && CSLCount(papszTokens) == 2 && OGROpenAirGetLatLon(papszTokens[0], dfFirstLat, dfFirstLon) && OGROpenAirGetLatLon(papszTokens[1], dfSecondLat, dfSecondLon)) { double dfStartDistance =OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfFirstLat, dfFirstLon); double dfEndDistance = OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfSecondLat, dfSecondLon); double dfStartAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfFirstLat, dfFirstLon); double dfEndAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfSecondLat, dfSecondLon); if (bClockWise && dfEndAngle < dfStartAngle) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; int nSign = (bClockWise) ? 1 : -1; double dfAngle; for(dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign) { double dfLat, dfLon; double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } oLR.addPoint(dfSecondLon, dfSecondLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } CSLDestroy(papszTokens); } else if ((EQUALN(pszLine, "DC ", 3) || EQUALN(pszLine, "DC=", 3)) && (bHasCenter || strstr(pszLine, "V X=") != NULL)) { if (!bHasCenter) { const char* pszVX = strstr(pszLine, "V X="); bHasCenter = OGROpenAirGetLatLon(pszVX, dfCenterLat, dfCenterLon); } if (bHasCenter) { pszLine += 3; double dfRADIUS = CPLAtof(pszLine) * 1852; double dfAngle; double dfLat, dfLon; for(dfAngle = 0; dfAngle < 360; dfAngle += 1) { OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, 0, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } } else if (EQUALN(pszLine, "V X=", 4)) { bHasCenter = OGROpenAirGetLatLon(pszLine + 4, dfCenterLat, dfCenterLon); } else if (EQUALN(pszLine, "V D=-", 5)) { bClockWise = FALSE; } else if (EQUALN(pszLine, "V D=+", 5)) { bClockWise = TRUE; } else { //CPLDebug("OpenAir", "Unexpected content : %s", pszLine); } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetField(0, osCLASS.c_str()); poFeature->SetField(1, osNAME.c_str()); poFeature->SetField(2, osFLOOR.c_str()); poFeature->SetField(3, osCEILING.c_str()); if (sStyle.penStyle != -1 || sStyle.fillR != -1) { CPLString osStyle; if (sStyle.penStyle != -1) { osStyle += CPLString().Printf("PEN(c:#%02X%02X%02X,w:%dpt", sStyle.penR, sStyle.penG, sStyle.penB, sStyle.penWidth); if (sStyle.penStyle == 1) osStyle += ",p:\"5px 5px\""; osStyle += ")"; } if (sStyle.fillR != -1) { if (osStyle.size() != 0) osStyle += ";"; osStyle += CPLString().Printf("BRUSH(fc:#%02X%02X%02X)", sStyle.fillR, sStyle.fillG, sStyle.fillB); } else { if (osStyle.size() != 0) osStyle += ";"; osStyle += "BRUSH(fc:#00000000,id:\"ogr-brush-1\")"; } if (osStyle.size() != 0) poFeature->SetStyleString(osStyle); } OGRPolygon* poPoly = new OGRPolygon(); oLR.closeRings(); poPoly->addRing(&oLR); poPoly->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
int SearchCSVForWKT( const char *pszFileCSV, const char *pszTarget ) { const char *pszFilename = NULL; const char *pszWKT = NULL; char szTemp[1024]; int nPos = 0; const char *pszTemp = NULL; VSILFILE *fp = NULL; OGRSpatialReference oSRS; int nCode = 0; int nFound = -1; CPLDebug( "gdalsrsinfo", "SearchCSVForWKT()\nfile=%s\nWKT=%s\n", pszFileCSV, pszTarget); /* -------------------------------------------------------------------- */ /* Find and open file. */ /* -------------------------------------------------------------------- */ // pszFilename = pszFileCSV; pszFilename = CPLFindFile( "gdal", pszFileCSV ); if( pszFilename == NULL ) { CPLDebug( "gdalsrsinfo", "could not find support file %s", pszFileCSV ); // return OGRERR_UNSUPPORTED_SRS; return -1; } /* support gzipped file */ if ( strstr( pszFileCSV,".gz") != NULL ) sprintf( szTemp, "/vsigzip/%s", pszFilename); else sprintf( szTemp, "%s", pszFilename); CPLDebug( "gdalsrsinfo", "SearchCSVForWKT() using file %s", szTemp ); fp = VSIFOpenL( szTemp, "r" ); if( fp == NULL ) { CPLDebug( "gdalsrsinfo", "could not open support file %s", pszFilename ); // return OGRERR_UNSUPPORTED_SRS; return -1; } /* -------------------------------------------------------------------- */ /* Process lines. */ /* -------------------------------------------------------------------- */ const char *pszLine; while( (pszLine = CPLReadLine2L(fp,-1,NULL)) != NULL ) { // CPLDebug( "gdalsrsinfo", "read line %s", pszLine ); if( pszLine[0] == '#' ) continue; /* do nothing */; // else if( EQUALN(pszLine,"include ",8) ) // { // eErr = importFromDict( pszLine + 8, pszCode ); // if( eErr != OGRERR_UNSUPPORTED_SRS ) // break; // } // else if( strstr(pszLine,",") == NULL ) // /* do nothing */; pszTemp = strstr(pszLine,","); if (pszTemp) { nPos = pszTemp - pszLine; if ( nPos == 0 ) continue; strncpy( szTemp, pszLine, nPos ); szTemp[nPos] = '\0'; nCode = atoi(szTemp); pszWKT = (char *) pszLine + nPos +1; // CPLDebug( "gdalsrsinfo", // "code=%d\nWKT=\n[%s]\ntarget=\n[%s]\n", // nCode,pszWKT, pszTarget ); if ( EQUAL(pszTarget,pszWKT) ) { nFound = nCode; CPLDebug( "gdalsrsinfo", "found EPSG:%d\n" "current=%s\ntarget= %s\n", nCode, pszWKT, pszTarget ); break; } } } VSIFCloseL( fp ); return nFound; }