Skip to content

Commit

Permalink
add support for shape files with non us-ascii filenames. (GPSBabel#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 authored Dec 13, 2017
1 parent abc5ffe commit 68c08f3
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 8 deletions.
113 changes: 107 additions & 6 deletions shape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,107 @@ arglist_t shp_args[] = {
ARG_TERMINATOR
};


/************************************************************************/
/* SHPOpenGpsbabel() */
/************************************************************************/

SHPHandle SHPAPI_CALL
SHPOpenGpsbabel( const QString& pszLayer, const char * pszAccess )

{
SAHooks sHooks;

#ifdef SHPAPI_UTF8_HOOKS
SASetupUtf8Hooks( &sHooks );
return SHPOpenLL( pszLayer.toUtf8().constData(), pszAccess, &sHooks );
#else
SASetupDefaultHooks( &sHooks );
return SHPOpenLL( qPrintable(pszLayer), pszAccess, &sHooks );
#endif

}

/************************************************************************/
/* SHPCreateGpsbabel() */
/* */
/* Create a new shape file and return a handle to the open */
/* shape file with read/write access. */
/************************************************************************/

SHPHandle SHPAPI_CALL
SHPCreateGpsbabel( const QString& pszLayer, int nShapeType )

{
SAHooks sHooks;

#ifdef SHPAPI_UTF8_HOOKS
SASetupUtf8Hooks( &sHooks );
return SHPCreateLL( pszLayer.toUtf8().constData(), nShapeType, &sHooks );
#else
SASetupDefaultHooks( &sHooks );
return SHPCreateLL( qPrintable(pszLayer), nShapeType, &sHooks );
#endif

}

/************************************************************************/
/* DBFOpenGpsbabel() */
/* */
/* Open a .dbf file. */
/************************************************************************/

DBFHandle SHPAPI_CALL
DBFOpenGpsbabel( const QString& pszFilename, const char * pszAccess )

{
SAHooks sHooks;

#ifdef SHPAPI_UTF8_HOOKS
SASetupUtf8Hooks( &sHooks );
return DBFOpenLL( pszFilename.toUtf8().constData(), pszAccess, &sHooks );
#else
SASetupDefaultHooks( &sHooks );
return DBFOpenLL( qPrintable(pszFilename), pszAccess, &sHooks );
#endif

}

/************************************************************************/
/* DBFCreateExGpsbabel() */
/* */
/* Create a new .dbf file. */
/************************************************************************/

DBFHandle SHPAPI_CALL
DBFCreateExGpsbabel( const QString& pszFilename, const char* pszCodePage )

{
SAHooks sHooks;

#ifdef SHPAPI_UTF8_HOOKS
SASetupUtf8Hooks( &sHooks );
return DBFCreateLL( pszFilename.toUtf8().constData(), pszCodePage , &sHooks );
#else
SASetupDefaultHooks( &sHooks );
return DBFCreateLL( qPrintable(pszFilename), pszCodePage , &sHooks );
#endif

}

/************************************************************************/
/* DBFCreateGpsbabel() */
/* */
/* Create a new .dbf file with default code page LDID/87 (0x57) */
/************************************************************************/

DBFHandle SHPAPI_CALL
DBFCreateGpsbabel( const QString& pszFilename )

{
return DBFCreateExGpsbabel( pszFilename, "LDID/87" ); // 0x57
}

static
void dump_fields(void)
{
Expand Down Expand Up @@ -99,12 +200,12 @@ my_rd_init(const QString& fname)
{
ifname = fname;
// TODO: The .prj file can define the the coordinate system and projection information.
ihandle = SHPOpen(qPrintable(fname), "rb");
ihandle = SHPOpenGpsbabel(fname, "rb");
if (ihandle == NULL) {
fatal(MYNAME ": Cannot open shp file %s for reading\n", qPrintable(ifname));
}

ihandledb = DBFOpen(qPrintable(fname), "rb");
ihandledb = DBFOpenGpsbabel(fname, "rb");
if (ihandledb == NULL) {
fatal(MYNAME ": Cannot open dbf file %s for reading\n", qPrintable(ifname));
}
Expand Down Expand Up @@ -376,13 +477,13 @@ my_write(void)
switch (global_opts.objective) {
case wptdata:
case unknown_gpsdata:
ohandle = SHPCreate(qPrintable(ofname), SHPT_POINT);
ohandle = SHPCreateGpsbabel(ofname, SHPT_POINT);

if (ohandle == NULL) {
fatal(MYNAME ": Cannot open shp file %s for writing\n",
qPrintable(ofname));
}
ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n");
ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n");
if (ohandledb == NULL) {
fatal(MYNAME ": Cannot open dbf file %s for writing\n",
qPrintable(ofname));
Expand All @@ -392,13 +493,13 @@ my_write(void)
break;
case rtedata:
case trkdata:
ohandle = SHPCreate(qPrintable(ofname), SHPT_ARC);
ohandle = SHPCreateGpsbabel(ofname, SHPT_ARC);

if (ohandle == NULL) {
fatal(MYNAME ": Cannot open shp file %s for writing\n",
qPrintable(ofname));
}
ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n");
ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n");
if (ohandledb == NULL) {
fatal(MYNAME ": Cannot open dbf file %s for writing\n",
qPrintable(ofname));
Expand Down
36 changes: 35 additions & 1 deletion test_encoding_latin1
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,42 @@ filenamebase=test_encoding_file
errorcount=`expr $errorcount + 1`
fi

# test input file name mangling with shape files
echo "testing input file name with shape files"
rm -f ${TMPDIR}/test_encoding_file*
for ext in cpg dbf prj shp shx
do
cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext}
done
${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || {
echo "ERROR: The input file name was mangled."
errorcount=`expr $errorcount + 1`
}

# test output file name mangling with shape files
echo "testing output file name with shape files"
rm -f ${TMPDIR}/test_encoding_file*
${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
for ext in cpg dbf shp shx
do
count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
if [ $count -lt 1 ]; then
echo "ERROR: The output file name was mangled."
errorcount=`expr $errorcount + 1`
fi
done
rm -f ${TMPDIR}/test_encoding_file*
${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
for ext in cpg dbf shp shx
do
count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
if [ $count -lt 1 ]; then
echo "ERROR: The output file name was mangled."
errorcount=`expr $errorcount + 1`
fi
done

# TODO: add tests to cover formats that use other open methods.
# for example shape files.

else
echo "$0 cannot run without the en_US.iso88591 locale."
Expand Down
36 changes: 35 additions & 1 deletion test_encoding_utf8
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,42 @@ filenamebase=test_encoding_fileαβγ
errorcount=`expr $errorcount + 1`
fi

# test input file name mangling with shape files
echo "testing input file name with shape files"
rm -f ${TMPDIR}/test_encoding_file*
for ext in cpg dbf prj shp shx
do
cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext}
done
${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || {
echo "ERROR: The input file name was mangled."
errorcount=`expr $errorcount + 1`
}

# test output file name mangling with shape files
echo "testing output file name with shape files"
rm -f ${TMPDIR}/test_encoding_file*
${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
for ext in cpg dbf shp shx
do
count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
if [ $count -lt 1 ]; then
echo "ERROR: The output file name was mangled."
errorcount=`expr $errorcount + 1`
fi
done
rm -f ${TMPDIR}/test_encoding_file*
${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
for ext in cpg dbf shp shx
do
count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
if [ $count -lt 1 ]; then
echo "ERROR: The output file name was mangled."
errorcount=`expr $errorcount + 1`
fi
done

# TODO: add tests to cover formats that use other open methods.
# for example shape files.

else
echo "$0 cannot run without the UTF-8 charmap."
Expand Down

0 comments on commit 68c08f3

Please sign in to comment.