#include "sdts_al.h"
#include "shapefil.h"
CPL_CVSID("$Id: sdts2shp.cpp 36770 2016-12-10 01:47:26Z goatbar $");
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;
}
}
}