Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Converters/Option1.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def translateCDB(cDBRoot,ogrPath, removeShapefile):
geoPackageFile = shapefile[0:-3] + "gpkg"
if(os.path.getsize(shapefile)>0):
#'-t_srs', 'EPSG:4326', '-s_srs', 'EPSG:4326',
subprocess.call([ogrPath,'-f', 'GPKG', geoPackageFile,shapefile])
subprocess.call([ogrPath, '-f', 'GPKG', geoPackageFile, "-lco", "FID=id", shapefile])
print(shapefile + ' -> ' + geoPackageFile)
if(removeShapefile):
converter.removeShapeFile(shapefile)
Expand Down
32 changes: 19 additions & 13 deletions Converters/Option1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def copyFeaturesFromShapeToGeoPackage(shpFilename, gpkgFilename):
outLayerName = getOutputLayerName(shpFilename)

ogrDriver = ogr.GetDriverByName("GPKG")
#print(" Creating file " + gpkgFilename)
gpkgFile = ogrDriver.CreateDataSource(gpkgFilename)

if(gpkgFile == None):
Expand All @@ -122,7 +123,7 @@ def copyFeaturesFromShapeToGeoPackage(shpFilename, gpkgFilename):
fieldIndexes[fieldName] = fieldIdx
fieldIdx += 1
else:
outLayer = gpkgFile.CreateLayer(outLayerName,srs,geom_type=layerDefinition.GetGeomType())
outLayer = gpkgFile.CreateLayer(outLayerName,srs,geom_type=layerDefinition.GetGeomType(),options=["FID=id"])

# Add fields
for i in range(layerDefinition.GetFieldCount()):
Expand Down Expand Up @@ -193,11 +194,12 @@ def copyFeaturesFromShapeToGeoPackage(shpFilename, gpkgFilename):
continue
fieldTypeCode = ogr.OFTString
if(isinstance(fieldValue,float)):
fieldTypeCode = ogr.OFSTFloat32
fieldTypeCode = ogr.OFTReal
if(isinstance(fieldValue,int)):
fieldTypeCode = ogr.OFTInteger
if(isinstance(fieldValue,bool)):
fieldTypeCode = ogr.OFSTBoolean
#DBase logical fields can have multiple values for true and false, best converted as text
#if(isinstance(fieldValue,bool)):
# fieldTypeCode = ogr.OFSTBoolean
fieldDef = ogr.FieldDefn(fieldName,fieldTypeCode)

outLayer.CreateField(fieldDef)
Expand All @@ -217,20 +219,24 @@ def copyFeaturesFromShapeToGeoPackage(shpFilename, gpkgFilename):
#Copy the geometry and attributes
outFeature.SetFrom(inFeature)

cnamValue = inFeature.GetField('CNAM')
fclassRecord = fClassRecords[cnamValue]
outFeature.SetField(fieldIndexes["_DATASET_CODE"], layerComponents['datasetcode'])
outFeature.SetField(fieldIndexes["_COMPONENT_SELECTOR_1"], layerComponents['selector1'])
outFeature.SetField(fieldIndexes["_COMPONENT_SELECTOR_2"], layerComponents['selector2'])
outFeature.SetField(fieldIndexes["_LOD"], layerComponents['lod'])
outFeature.SetField(fieldIndexes["_UREF"], layerComponents['uref'])
outFeature.SetField(fieldIndexes["_RREF"], layerComponents['rref'])

#flatten attributes from the feature class attributes table
if(cnamValue in fClassRecords.keys()):
fclassFields = fClassRecords[cnamValue]
for field in fclassFields.keys():
outFeature.SetField(fieldIndexes[field],fclassFields[field])
#flatten attributes from the feature class attributes table, if a CNAM attribute exists
try:
cnamValue = inFeature.GetField('CNAM')
fclassRecord = fClassRecords[cnamValue]
if(cnamValue in fClassRecords.keys()):
fclassFields = fClassRecords[cnamValue]
for field in fclassFields.keys():
outFeature.SetField(fieldIndexes[field],fclassFields[field])
except:
#print(" File does not contain the CNAM attribute")
cnamValue = ""

#write the feature
outLayer.CreateFeature(outFeature)
Expand Down Expand Up @@ -281,13 +287,13 @@ def convertShapeFile(shpFilename, cdbInputDir, cdbOutputDir):

#Read all the feature records from the DBF at once (using GDAL)
#copyFeaturesFromShapeToGeoPackage(shpFilename,outputGeoPackageFile)
fClassRecords = converter.readDBF(fcAttrName)
#fClassRecords = converter.readDBF(fcAttrName)
#Read Featureclass records
featureTableName = converter.getFeatureAttrTableName(shpFilename)
copyFeaturesFromShapeToGeoPackage(shpFilename,outputGeoPackageFile)
#convertSHP(sqliteCon,shpFilename,outputGeoPackageFile, fClassRecords, True)
sqliteCon = sqlite3.connect(outputGeoPackageFile)
if(createExtendedAttributesTable(sqliteCon,shpFilename)):
if(0 and createExtendedAttributesTable(sqliteCon,shpFilename)):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryan, could you please explain why you want to disable the above if statement ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code crashes on Yemen (Tiles\N12\E044\201_RoadNetwork\L00\U0\N12E044_D201_S002_T003_L00_U0_R0.shp). It is trying to get the CNAM attribute, but from an extended attribute DBF file where one doesn't exist. Since it wasn't converting correctly anyway, I disabled it.

dbfTableName = getExtendedAttrTableName(shpFilename)
RelatedTables.createRTESchema(sqliteCon)
relationship = RelatedTables.Relationship()
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ _use at your own risk_.
Please contact [email protected] with any questions, comments, pull requests,
etc.

## Converter Procedure

* Software Installations
* Download and install python 3.7 (I think the script was tested with 3.6, but 3.7 has worked good)
* https://www.python.org/ftp/python/3.7.2/python-3.7.2-amd64.exe
* Download and unzip GDAL executable somewhere on disk (if you don’t already have it)
* http://download.gisinternals.com/sdk/downloads/release-1900-x64-gdal-2-4-0-mapserver-7-2-2.zip
* Download and install the GDAL python package
* http://download.gisinternals.com/sdk/downloads/release-1900-x64-gdal-2-4-0-mapserver-7-2-2/GDAL-2.4.0.win-amd64-py3.7.msi
* Download and install the dbfread python library
* https://files.pythonhosted.org/packages/4c/94/51349e43503e30ed7b4ecfe68a8809cdb58f722c0feb79d18b1f1e36fe74/dbfread-2.0.7-py2.py3-none-any.whl
* python -m pip install _pathToDownloadedFileAbove_
* There is probably easier ways to install these python packages, but our work internet breaks the easier methods that auto download and install
* Running the converter
* Add the GDAL bin directory in the path (first, in case other programs have older versions of GDAL)
* set PATH=_gdalBinDir_;%PATH%
* set GDAL_DATA=_gdalBinDir_\gdal-data
* Run the conversion
* pathToConverters\Option1d.py _PathToInputCDB_ _PathToOutputCDB_

---
Copyright 2018, US Army Geospatial Center, Leidos Inc., and Cognitics Inc.

Expand Down