📄 ogr_srs_esri.cpp
字号:
if( papszDatumMapping != apszDefaultDatumMapping )
{
CSLDestroy( papszDatumMapping );
papszDatumMapping = NULL;
}
}
CPL_C_END
/************************************************************************/
/* InitDatumMappingTable() */
/************************************************************************/
static void InitDatumMappingTable()
{
if( papszDatumMapping != NULL )
return;
/* -------------------------------------------------------------------- */
/* Try to open the datum.csv file. */
/* -------------------------------------------------------------------- */
const char *pszFilename = CSVFilename("gdal_datum.csv");
FILE * fp = VSIFOpen( pszFilename, "rb" );
/* -------------------------------------------------------------------- */
/* Use simple default set if we can't find the file. */
/* -------------------------------------------------------------------- */
if( fp == NULL )
{
papszDatumMapping = apszDefaultDatumMapping;
return;
}
/* -------------------------------------------------------------------- */
/* Figure out what fields we are interested in. */
/* -------------------------------------------------------------------- */
char **papszFieldNames = CSVReadParseLine( fp );
int nDatumCodeField = CSLFindString( papszFieldNames, "DATUM_CODE" );
int nEPSGNameField = CSLFindString( papszFieldNames, "DATUM_NAME" );
int nESRINameField = CSLFindString( papszFieldNames, "ESRI_DATUM_NAME" );
CSLDestroy( papszFieldNames );
if( nDatumCodeField == -1 || nEPSGNameField == -1 || nESRINameField == -1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Failed to find required field in gdal_datum.csv in InitDatumMappingTable(), using default table setup." );
papszDatumMapping = apszDefaultDatumMapping;
return;
}
/* -------------------------------------------------------------------- */
/* Read each line, adding a detail line for each. */
/* -------------------------------------------------------------------- */
int nMappingCount = 0;
const int nMaxDatumMappings = 1000;
char **papszFields;
papszDatumMapping = (char **)CPLCalloc(sizeof(char*),nMaxDatumMappings*3);
for( papszFields = CSVReadParseLine( fp );
papszFields != NULL;
papszFields = CSVReadParseLine( fp ) )
{
int nFieldCount = CSLCount(papszFields);
CPLAssert( nMappingCount+1 < nMaxDatumMappings );
if( MAX(nEPSGNameField,MAX(nDatumCodeField,nESRINameField))
< nFieldCount
&& nMaxDatumMappings > nMappingCount+1 )
{
papszDatumMapping[nMappingCount*3+0] =
CPLStrdup( papszFields[nDatumCodeField] );
papszDatumMapping[nMappingCount*3+1] =
CPLStrdup( papszFields[nESRINameField] );
papszDatumMapping[nMappingCount*3+2] =
CPLStrdup( papszFields[nEPSGNameField] );
OGREPSGDatumNameMassage( &(papszDatumMapping[nMappingCount*3+2]) );
nMappingCount++;
}
CSLDestroy( papszFields );
}
VSIFClose( fp );
papszDatumMapping[nMappingCount*3+0] = NULL;
papszDatumMapping[nMappingCount*3+1] = NULL;
papszDatumMapping[nMappingCount*3+2] = NULL;
}
/************************************************************************/
/* OSRImportFromESRI() */
/************************************************************************/
OGRErr OSRImportFromESRI( OGRSpatialReferenceH hSRS, char **papszPrj )
{
return ((OGRSpatialReference *) hSRS)->importFromESRI( papszPrj );
}
/************************************************************************/
/* OSR_GDV() */
/* */
/* Fetch a particular parameter out of the parameter list, or */
/* the indicated default if it isn't available. This is a */
/* helper function for importFromESRI(). */
/************************************************************************/
static double OSR_GDV( char **papszNV, const char * pszField,
double dfDefaultValue )
{
int iLine;
if( papszNV == NULL || papszNV[0] == NULL )
return dfDefaultValue;
if( EQUALN(pszField,"PARAM_",6) )
{
int nOffset;
for( iLine = 0;
papszNV[iLine] != NULL && !EQUALN(papszNV[iLine],"Paramet",7);
iLine++ ) {}
for( nOffset=atoi(pszField+6);
papszNV[iLine] != NULL && nOffset > 0;
iLine++ )
{
if( strlen(papszNV[iLine]) > 0 )
nOffset--;
}
while( papszNV[iLine] != NULL && strlen(papszNV[iLine]) == 0 )
iLine++;
if( papszNV[iLine] != NULL )
{
char **papszTokens, *pszLine = papszNV[iLine];
double dfValue;
int i;
// Trim comments.
for( i=0; pszLine[i] != '\0'; i++ )
{
if( pszLine[i] == '/' && pszLine[i+1] == '*' )
pszLine[i] = '\0';
}
papszTokens = CSLTokenizeString(papszNV[iLine]);
if( CSLCount(papszTokens) == 3 )
{
dfValue = ABS(atof(papszTokens[0]))
+ atof(papszTokens[1]) / 60.0
+ atof(papszTokens[2]) / 3600.0;
if( atof(papszTokens[0]) < 0.0 )
dfValue *= -1;
}
else if( CSLCount(papszTokens) > 0 )
dfValue = atof(papszTokens[0]);
else
dfValue = dfDefaultValue;
CSLDestroy( papszTokens );
return dfValue;
}
else
return dfDefaultValue;
}
else
{
for( iLine = 0;
papszNV[iLine] != NULL &&
!EQUALN(papszNV[iLine],pszField,strlen(pszField));
iLine++ ) {}
if( papszNV[iLine] == NULL )
return dfDefaultValue;
else
return atof( papszNV[iLine] + strlen(pszField) );
}
}
/************************************************************************/
/* OSR_GDS() */
/************************************************************************/
static const char*OSR_GDS( char **papszNV, const char * pszField,
const char *pszDefaultValue )
{
int iLine;
if( papszNV == NULL || papszNV[0] == NULL )
return pszDefaultValue;
for( iLine = 0;
papszNV[iLine] != NULL &&
!EQUALN(papszNV[iLine],pszField,strlen(pszField));
iLine++ ) {}
if( papszNV[iLine] == NULL )
return pszDefaultValue;
else
{
static char szResult[80];
char **papszTokens;
papszTokens = CSLTokenizeString(papszNV[iLine]);
if( CSLCount(papszTokens) > 1 )
strncpy( szResult, papszTokens[1], sizeof(szResult));
else
strncpy( szResult, pszDefaultValue, sizeof(szResult));
CSLDestroy( papszTokens );
return szResult;
}
}
/************************************************************************/
/* importFromESRI() */
/************************************************************************/
/**
* Import coordinate system from ESRI .prj format(s).
*
* This function will read the text loaded from an ESRI .prj file, and
* translate it into an OGRSpatialReference definition. This should support
* many (but by no means all) old style (Arc/Info 7.x) .prj files, as well
* as the newer pseudo-OGC WKT .prj files. Note that new style .prj files
* are in OGC WKT format, but require some manipulation to correct datum
* names, and units on some projection parameters. This is addressed within
* importFromESRI() by an automatical call to morphFromESRI().
*
* Currently only GEOGRAPHIC, UTM, STATEPLANE, GREATBRITIAN_GRID, ALBERS,
* EQUIDISTANT_CONIC, and TRANSVERSE (mercator) projections are supported
* from old style files.
*
* At this time there is no equivelent exportToESRI() method. Writing old
* style .prj files is not supported by OGRSpatialReference. However the
* morphToESRI() and exportToWkt() methods can be used to generate output
* suitable to write to new style (Arc 8) .prj files.
*
* This function is the equilvelent of the C function OSRImportFromESRI().
*
* @param papszPrj NULL terminated list of strings containing the definition.
*
* @return OGRERR_NONE on success or an error code in case of failure.
*/
OGRErr OGRSpatialReference::importFromESRI( char **papszPrj )
{
if( papszPrj == NULL || papszPrj[0] == NULL )
return OGRERR_CORRUPT_DATA;
/* -------------------------------------------------------------------- */
/* ArcGIS and related products now use a varient of Well Known */
/* Text. Try to recognise this and ingest it. WKT is usually */
/* all on one line, but we will accept multi-line formats and */
/* concatenate. */
/* -------------------------------------------------------------------- */
if( EQUALN(papszPrj[0],"GEOGCS",6)
|| EQUALN(papszPrj[0],"PROJCS",6)
|| EQUALN(papszPrj[0],"LOCAL_CS",8) )
{
char *pszWKT, *pszWKT2;
OGRErr eErr;
int i;
pszWKT = CPLStrdup(papszPrj[0]);
for( i = 1; papszPrj[i] != NULL; i++ )
{
pszWKT = (char *)
CPLRealloc(pszWKT,strlen(pszWKT)+strlen(papszPrj[i])+1);
strcat( pszWKT, papszPrj[i] );
}
pszWKT2 = pszWKT;
eErr = importFromWkt( &pszWKT2 );
CPLFree( pszWKT );
if( eErr == OGRERR_NONE )
eErr = morphFromESRI();
return eErr;
}
/* -------------------------------------------------------------------- */
/* Operate on the basis of the projection name. */
/* -------------------------------------------------------------------- */
const char *pszProj = OSR_GDS( papszPrj, "Projection", NULL );
if( pszProj == NULL )
{
CPLDebug( "OGR_ESRI", "Can't find Projection\n" );
return OGRERR_CORRUPT_DATA;
}
else if( EQUAL(pszProj,"GEOGRAPHIC") )
{
}
else if( EQUAL(pszProj,"utm") )
{
if( (int) OSR_GDV( papszPrj, "zone", 0.0 ) != 0 )
{
double dfYShift = OSR_GDV( papszPrj, "Yshift", 0.0 );
SetUTM( (int) OSR_GDV( papszPrj, "zone", 0.0 ),
dfYShift == 0.0 );
}
else
{
double dfCentralMeridian, dfRefLat;
int nZone;
dfCentralMeridian = OSR_GDV( papszPrj, "PARAM_1", 0.0 );
dfRefLat = OSR_GDV( papszPrj, "PARAM_2", 0.0 );
nZone = (int) ((dfCentralMeridian+183) / 6.0 + 0.0000001);
SetUTM( nZone, dfRefLat >= 0.0 );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -