#include "sdts_al.h"
#include "shapefil.h"
CPL_CVSID("$Id$")
static int bVerbose = FALSE;
static
void WriteLineShapefile( const
char *,
SDTSTransfer *,
const char * );
static
void WritePointShapefile( const
char *,
SDTSTransfer *,
const char * );
static
void WriteAttributeDBF( const
char *,
SDTSTransfer *,
const char * );
static
void WritePolygonShapefile( const
char *,
SDTSTransfer *,
const char * );
static void
AddPrimaryAttrToDBFSchema( DBFHandle hDBF,
SDTSTransfer * poTransfer,
char ** papszModuleList );
static void
WritePrimaryAttrToDBF( DBFHandle hDBF, int nRecord,
static void
WriteAttrRecordToDBF( DBFHandle hDBF, int nRecord,
static void Usage()
{
printf( "Usage: sdts2shp CATD_filename [-o shapefile_name]\n"
" [-m module_name] [-v]\n"
"\n"
"Modules include `LE01', `PC01', `NP01' and `ARDF'\n" );
exit( 1 );
}
int main( int nArgc, char ** papszArgv )
{
{
int i;
const char *pszCATDFilename = NULL;
const char *pszMODN = "LE01";
char *pszShapefile = "sdts_out.shp";
if( nArgc < 2 )
Usage();
pszCATDFilename = papszArgv[1];
for( i = 2; i < nArgc; i++ )
{
if(
EQUAL(papszArgv[i],
"-m") && i+1 < nArgc )
pszMODN = papszArgv[++i];
else if(
EQUAL(papszArgv[i],
"-o") && i+1 < nArgc )
pszShapefile = papszArgv[++i];
else if(
EQUAL(papszArgv[i],
"-v") )
bVerbose = TRUE;
else
{
printf( "Incomplete, or unsupported option `%s'\n\n",
papszArgv[i] );
Usage();
}
}
for( i = strlen(pszShapefile)-1; i >= 0; i-- )
{
if( pszShapefile[i] == '.' )
{
pszShapefile[i] = '\0';
break;
}
else if( pszShapefile[i] == '/' || pszShapefile[i] == '\\' )
break;
}
if( !oTransfer.
Open( pszCATDFilename ) )
{
fprintf( stderr,
"Failed to read CATD file `%s'\n",
pszCATDFilename );
exit( 100 );
}
if( bVerbose )
{
printf( "Layers:\n" );
for( i = 0; i < oTransfer.GetLayerCount(); i++ )
{
printf( " %s: `%s'\n",
oTransfer.
GetCATD()->GetEntryModule(iCATDEntry),
}
printf( "\n" );
}
{
fprintf( stderr, "Unable to identify module: %s\n", pszMODN );
exit( 1 );
}
if( pszMODN[0] == 'L' || pszMODN[0] == 'l' )
{
WriteLineShapefile( pszShapefile, &oTransfer, pszMODN );
}
else if( pszMODN[0] == 'A' || pszMODN[0] == 'a'
|| pszMODN[0] == 'B' || pszMODN[0] == 'b' )
{
WriteAttributeDBF( pszShapefile, &oTransfer, pszMODN );
}
else if( pszMODN[0] == 'N' || pszMODN[0] == 'n' )
{
WritePointShapefile( pszShapefile, &oTransfer, pszMODN );
}
else if( pszMODN[0] == 'P' || pszMODN[0] == 'p' )
{
WritePolygonShapefile( pszShapefile, &oTransfer, pszMODN );
}
else
{
fprintf( stderr, "Unrecognized module name: %s\n", pszMODN );
}
}
#ifdef DBMALLOC
malloc_dump(1);
#endif
}
static void WriteLineShapefile( const char * pszShapefile,
const char * pszMODN )
{
if( poLineReader == NULL )
{
fprintf( stderr, "Failed to open %s.\n",
poTransfer->
GetCATD()->GetModuleFilePath( pszMODN ) );
return;
}
SHPHandle hSHP;
hSHP = SHPCreate( pszShapefile, SHPT_ARC );
if( hSHP == NULL )
{
fprintf( stderr, "Unable to create shapefile `%s'\n",
pszShapefile );
return;
}
DBFHandle hDBF;
int nLeftPolyField, nRightPolyField;
int nStartNodeField, nEndNodeField, nSDTSRecordField;
char szDBFFilename[1024];
sprintf( szDBFFilename, "%s.dbf", pszShapefile );
hDBF = DBFCreate( szDBFFilename );
if( hDBF == NULL )
{
fprintf( stderr, "Unable to create shapefile .dbf for `%s'\n",
pszShapefile );
return;
}
nSDTSRecordField = DBFAddField( hDBF, "SDTSRecId", FTInteger, 8, 0 );
nLeftPolyField = DBFAddField( hDBF, "LeftPoly", FTString, 12, 0 );
nRightPolyField = DBFAddField( hDBF, "RightPoly", FTString, 12, 0 );
nStartNodeField = DBFAddField( hDBF, "StartNode", FTString, 12, 0 );
nEndNodeField = DBFAddField( hDBF, "EndNode", FTString, 12, 0 );
AddPrimaryAttrToDBFSchema( hDBF, poTransfer, papszModRefs );
while( (poRawLine = poLineReader->GetNextLine()) != NULL )
{
SHPObject *psShape =
SHPCreateSimpleObject( SHPT_ARC, poRawLine->
nVertices,
int iShape = SHPWriteObject( hSHP, -1, psShape );
SHPDestroyObject( psShape );
char szID[13];
DBFWriteIntegerAttribute( hDBF, iShape, nSDTSRecordField,
sprintf( szID, "%s:%d",
DBFWriteStringAttribute( hDBF, iShape, nLeftPolyField, szID );
sprintf( szID, "%s:%d",
DBFWriteStringAttribute( hDBF, iShape, nRightPolyField, szID );
sprintf( szID, "%s:%d",
DBFWriteStringAttribute( hDBF, iShape, nStartNodeField, szID );
sprintf( szID, "%s:%d",
DBFWriteStringAttribute( hDBF, iShape, nEndNodeField, szID );
WritePrimaryAttrToDBF( hDBF, iShape, poTransfer, poRawLine );
delete poRawLine;
}
DBFClose( hDBF );
SHPClose( hSHP );
}
static void WritePointShapefile( const char * pszShapefile,
const char * pszMODN )
{
if( poPointReader == NULL )
{
fprintf( stderr, "Failed to open %s.\n",
poTransfer->
GetCATD()->GetModuleFilePath( pszMODN ) );
return;
}
SHPHandle hSHP;
hSHP = SHPCreate( pszShapefile, SHPT_POINT );
if( hSHP == NULL )
{
fprintf( stderr, "Unable to create shapefile `%s'\n",
pszShapefile );
return;
}
DBFHandle hDBF;
int nAreaField, nSDTSRecordField;
char szDBFFilename[1024];
sprintf( szDBFFilename, "%s.dbf", pszShapefile );
hDBF = DBFCreate( szDBFFilename );
if( hDBF == NULL )
{
fprintf( stderr, "Unable to create shapefile .dbf for `%s'\n",
pszShapefile );
return;
}
nSDTSRecordField = DBFAddField( hDBF, "SDTSRecId", FTInteger, 8, 0 );
nAreaField = DBFAddField( hDBF, "AreaId", FTString, 12, 0 );
AddPrimaryAttrToDBFSchema( hDBF, poTransfer, papszModRefs );
while( (poRawPoint = poPointReader->GetNextPoint()) != NULL )
{
SHPObject *psShape =
SHPCreateSimpleObject( SHPT_POINT, 1,
int iShape = SHPWriteObject( hSHP, -1, psShape );
SHPDestroyObject( psShape );
char szID[13];
DBFWriteIntegerAttribute( hDBF, iShape, nSDTSRecordField,
sprintf( szID, "%s:%d",
DBFWriteStringAttribute( hDBF, iShape, nAreaField, szID );
WritePrimaryAttrToDBF( hDBF, iShape, poTransfer, poRawPoint );
delete poRawPoint;
}
DBFClose( hDBF );
SHPClose( hSHP );
}
static void WriteAttributeDBF( const char * pszShapefile,
const char * pszMODN )
{
if( poAttrReader == NULL )
{
fprintf( stderr, "Failed to open %s.\n",
poTransfer->
GetCATD()->GetModuleFilePath( pszMODN ) );
return;
}
DBFHandle hDBF;
char szDBFFilename[1024];
sprintf( szDBFFilename, "%s.dbf", pszShapefile );
hDBF = DBFCreate( szDBFFilename );
if( hDBF == NULL )
{
fprintf( stderr, "Unable to create shapefile .dbf for `%s'\n",
pszShapefile );
return;
}
DBFAddField( hDBF, "SDTSRecId", FTInteger, 8, 0 );
AddPrimaryAttrToDBFSchema( hDBF, poTransfer, papszMODNList );
int iRecord = 0;
!= NULL )
{
DBFWriteIntegerAttribute( hDBF, iRecord, 0,
WriteAttrRecordToDBF( hDBF, iRecord, poTransfer, poRecord->
poATTR );
delete poRecord;
iRecord++;
}
DBFClose( hDBF );
}
static void WritePolygonShapefile( const char * pszShapefile,
const char * pszMODN )
{
if( poPolyReader == NULL )
{
fprintf( stderr, "Failed to open %s.\n",
poTransfer->
GetCATD()->GetModuleFilePath( pszMODN ) );
return;
}
SHPHandle hSHP;
hSHP = SHPCreate( pszShapefile, SHPT_POLYGON );
if( hSHP == NULL )
{
fprintf( stderr, "Unable to create shapefile `%s'\n",
pszShapefile );
return;
}
DBFHandle hDBF;
int nSDTSRecordField;
char szDBFFilename[1024];
sprintf( szDBFFilename, "%s.dbf", pszShapefile );
hDBF = DBFCreate( szDBFFilename );
if( hDBF == NULL )
{
fprintf( stderr, "Unable to create shapefile .dbf for `%s'\n",
pszShapefile );
return;
}
nSDTSRecordField = DBFAddField( hDBF, "SDTSRecId", FTInteger, 8, 0 );
AddPrimaryAttrToDBFSchema( hDBF, poTransfer, papszModRefs );
!= NULL )
{
SHPObject *psShape =
SHPCreateObject( SHPT_POLYGON, -1, poRawPoly->
nRings,
NULL );
int iShape = SHPWriteObject( hSHP, -1, psShape );
SHPDestroyObject( psShape );
DBFWriteIntegerAttribute( hDBF, iShape, nSDTSRecordField,
WritePrimaryAttrToDBF( hDBF, iShape, poTransfer, poRawPoly );
delete poRawPoly;
}
DBFClose( hDBF );
SHPClose( hSHP );
}
static void
AddPrimaryAttrToDBFSchema( DBFHandle hDBF,
SDTSTransfer *poTransfer,
char ** papszModuleList )
{
for( int iModule = 0;
papszModuleList != NULL && papszModuleList[iModule] != NULL;
iModule++ )
{
poTransfer->
FindLayer( papszModuleList[iModule] ) );
if( poAttrReader == NULL )
{
printf( "Unable to open attribute module %s, skipping.\n" ,
papszModuleList[iModule] );
continue;
}
if( poAttrFeature == NULL )
{
fprintf( stderr,
"Didn't find any meaningful attribute records in %s.\n",
papszModuleList[iModule] );
continue;
}
int iSF;
{
{
case DDFString:
if( nWidth == 0 )
{
int nMaxBytes;
&nMaxBytes);
nMaxBytes, NULL ));
}
DBFAddField( hDBF, poSFDefn->
GetName(), FTString, nWidth, 0 );
break;
case DDFInt:
if( nWidth == 0 )
nWidth = 9;
DBFAddField( hDBF, poSFDefn->
GetName(), FTInteger, nWidth, 0 );
break;
case DDFFloat:
DBFAddField( hDBF, poSFDefn->
GetName(), FTDouble, 18, 6 );
break;
default:
fprintf( stderr,
"Dropping attribute `%s' of module `%s'. "
"Type unsupported\n",
papszModuleList[iModule] );
break;
}
}
delete poAttrFeature;
}
}
static void
WritePrimaryAttrToDBF( DBFHandle hDBF, int iRecord,
{
for( int iAttrRecord = 0;
iAttrRecord++ )
{
WriteAttrRecordToDBF( hDBF, iRecord, poTransfer, poSR );
}
}
static void
WriteAttrRecordToDBF( DBFHandle hDBF, int iRecord,
{
{
int iField;
int nMaxBytes;
&nMaxBytes);
for( iField = 0; iField < hDBF->nFields; iField++ )
{
hDBF->pszHeader+iField*32,10) )
break;
}
if( iField == hDBF->nFields )
iField = -1;
{
case DDFString:
const char *pszValue
if( iField != -1 )
DBFWriteStringAttribute(hDBF, iRecord, iField, pszValue );
break;
case DDFFloat:
double dfValue;
NULL);
if( iField != -1 )
DBFWriteDoubleAttribute( hDBF, iRecord, iField, dfValue );
break;
case DDFInt:
int nValue;
if( iField != -1 )
DBFWriteIntegerAttribute( hDBF, iRecord, iField, nValue );
break;
default:
break;
}
}
}
Definition: iso8211.h:183
int GetSubfieldCount() const
Definition: iso8211.h:214
DDFSubfieldDefn * GetSubfield(int i)
Definition: ddffielddefn.cpp:910
Definition: iso8211.h:512
const char * GetSubfieldData(DDFSubfieldDefn *, int *=nullptr, int=0)
Definition: ddffield.cpp:152
DDFFieldDefn * GetFieldDefn()
Definition: iso8211.h:538
Definition: iso8211.h:289
DDFDataType GetType() const
Definition: iso8211.h:312
int ExtractIntData(const char *pachData, int nMaxBytes, int *pnConsumedBytes)
Definition: ddfsubfielddefn.cpp:598
const char * GetName() const
Definition: iso8211.h:298
double ExtractFloatData(const char *pachData, int nMaxBytes, int *pnConsumedBytes)
Definition: ddfsubfielddefn.cpp:456
int GetWidth() const
Definition: iso8211.h:334
const char * ExtractStringData(const char *pachData, int nMaxBytes, int *pnConsumedBytes)
Definition: ddfsubfielddefn.cpp:400
Definition: sdts_al.h:373
Definition: sdts_al.h:347
DDFField * poATTR
Definition: sdts_al.h:358
Definition: sdts_al.h:198
SDTSModId * paoATID
Definition: sdts_al.h:212
int nAttributes
Definition: sdts_al.h:208
SDTSModId oModId
Definition: sdts_al.h:205
virtual void Rewind()
Definition: sdtsindexedreader.cpp:260
int IsIndexed() const
Definition: sdtsindexedreader.cpp:68
char ** ScanModuleReferences(const char *="ATID")
Definition: sdtsindexedreader.cpp:244
SDTSFeature * GetNextFeature()
Definition: sdtsindexedreader.cpp:119
Definition: sdts_al.h:317
char szModule[8]
Definition: sdts_al.h:175
int nRecord
Definition: sdts_al.h:179
Definition: sdts_al.h:434
Definition: sdts_al.h:516
void AssembleRings(SDTSTransfer *, int iPolyLayer)
Definition: sdtspolygonreader.cpp:594
Definition: sdts_al.h:266
SDTSModId oStartNode
Definition: sdts_al.h:293
SDTSModId oEndNode
Definition: sdts_al.h:297
double * padfZ
Definition: sdts_al.h:281
SDTSModId oLeftPoly
Definition: sdts_al.h:285
int nVertices
Definition: sdts_al.h:274
double * padfX
Definition: sdts_al.h:277
SDTSModId oRightPoly
Definition: sdts_al.h:289
double * padfY
Definition: sdts_al.h:279
Definition: sdts_al.h:404
double dfZ
Definition: sdts_al.h:416
double dfY
Definition: sdts_al.h:414
SDTSModId oAreaId
Definition: sdts_al.h:419
double dfX
Definition: sdts_al.h:412
Definition: sdts_al.h:470
int nVertices
Definition: sdts_al.h:491
int * panRingStart
Definition: sdts_al.h:494
int nRings
Definition: sdts_al.h:489
double * padfX
Definition: sdts_al.h:498
double * padfZ
Definition: sdts_al.h:504
double * padfY
Definition: sdts_al.h:501
Definition: sdts_al.h:616
SDTSIndexedReader * GetLayerIndexedReader(int)
Definition: sdtstransfer.cpp:428
SDTS_CATD * GetCATD()
Definition: sdts_al.h:643
DDFField * GetAttr(SDTSModId *)
Definition: sdtstransfer.cpp:537
int Open(const char *)
Definition: sdtstransfer.cpp:68
int GetLayerCATDEntry(int) const
Definition: sdtstransfer.cpp:213
int FindLayer(const char *)
Definition: sdtstransfer.cpp:472
const char * GetEntryTypeDesc(int) const
Definition: sdtscatd.cpp:242
#define CPLFree
Definition: cpl_conv.h:81
char CPL_DLL * CPLStrdup(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL
Definition: cpl_conv.cpp:293
#define EQUAL(a, b)
Definition: cpl_port.h:559
#define EQUALN(a, b, n)
Definition: cpl_port.h:557
void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList)
Definition: cpl_string.cpp:200
CPL_C_START char CPL_DLL ** CSLAddString(char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT
Definition: cpl_string.cpp:83