diff --git a/lib/ogr2ogr.py b/lib/ogr2ogr.py index f3211ce..f26f4c6 100644 --- a/lib/ogr2ogr.py +++ b/lib/ogr2ogr.py @@ -1,16 +1,16 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- -#***************************************************************************** -# $Id$ +# ***************************************************************************** +# $Id: ogr2ogr.py 428d6fbc987332afb0ba6c7b6913390f7386e864 2020-01-17 22:19:28 +0100 Even Rouault $ # # Project: OpenGIS Simple Features Reference Implementation # Purpose: Python port of a simple client for translating between formats. -# Author: Even Rouault, +# Author: Even Rouault, # # Port from ogr2ogr.cpp whose author is Frank Warmerdam # -#***************************************************************************** -# Copyright (c) 2010-2013, Even Rouault +# ***************************************************************************** +# Copyright (c) 2010-2013, Even Rouault # Copyright (c) 1999, Frank Warmerdam # # Permission is hereby granted, free of charge, to any person obtaining a @@ -30,7 +30,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -#************************************************************************** +# ************************************************************************** # Note : this is the most direct port of ogr2ogr.cpp possible # It could be made much more Python'ish ! @@ -39,21 +39,27 @@ import os import stat +#os.environ["GDAL_LIBRARY_PATH"] = "/usr/local/gdal/gdal-3.2.2/lib/libgdal.so" + from osgeo import gdal from osgeo import ogr from osgeo import osr +ogr.UseExceptions() + ############################################################################### -class ScaledProgressObject: - def __init__(self, min, max, cbk, cbk_data = None): - self.min = min - self.max = max + +class ScaledProgressObject(object): + def __init__(self, mini, maxi, cbk, cbk_data=None): + self.min = mini + self.max = maxi self.cbk = cbk self.cbk_data = cbk_data ############################################################################### + def ScaledProgressFunc(pct, msg, data): if data.cbk is None: return True @@ -61,6 +67,7 @@ def ScaledProgressFunc(pct, msg, data): ############################################################################### + def EQUAL(a, b): return a.lower() == b.lower() @@ -68,12 +75,15 @@ def EQUAL(a, b): # Redefinition of GDALTermProgress, so that autotest/pyscripts/test_ogr2ogr_py.py # can check that the progress bar is displayed + nLastTick = -1 -def TermProgress( dfComplete, pszMessage, pProgressArg ): + +def TermProgress(dfComplete, pszMessage, pProgressArg): + # pylint: disable=unused-argument global nLastTick - nThisTick = (int) (dfComplete * 40.0) + nThisTick = int(dfComplete * 40.0) if nThisTick < 0: nThisTick = 0 @@ -95,43 +105,49 @@ def TermProgress( dfComplete, pszMessage, pProgressArg ): sys.stdout.write('.') if nThisTick == 40: - print(" - done." ) + print(" - done.") else: sys.stdout.flush() return True -class TargetLayerInfo: + +class TargetLayerInfo(object): def __init__(self): self.poDstLayer = None self.poCT = None - #self.papszTransformOptions = None + # self.papszTransformOptions = None self.panMap = None self.iSrcZField = None -class AssociatedLayers: + +class AssociatedLayers(object): def __init__(self): self.poSrcLayer = None self.psInfo = None -#********************************************************************** +# ********************************************************************** # main() -#********************************************************************** +# ********************************************************************** + bSkipFailures = False nGroupTransactions = 200 bPreserveFID = False nFIDToFetch = ogr.NullFID + class Enum(set): def __getattr__(self, name): if name in self: return name raise AttributeError + GeomOperation = Enum(["NONE", "SEGMENTIZE", "SIMPLIFY_PRESERVE_TOPOLOGY"]) -def main(args = None, progress_func = TermProgress, progress_data = None): + +def main(args=None, progress_func=TermProgress, progress_data=None): global bSkipFailures global nGroupTransactions @@ -179,8 +195,8 @@ def main(args = None, progress_func = TermProgress, progress_data = None): pszClipDstSQL = None pszClipDstLayer = None pszClipDstWhere = None - #pszSrcEncoding = None - #pszDstEncoding = None + # pszSrcEncoding = None + # pszDstEncoding = None bWrapDateline = False bExplodeCollections = False pszZField = None @@ -189,7 +205,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): if args is None: args = sys.argv - args = ogr.GeneralCmdLineProcessor( args ) + args = ogr.GeneralCmdLineProcessor(args) # -------------------------------------------------------------------- # Processing command line arguments. @@ -201,110 +217,110 @@ def main(args = None, progress_func = TermProgress, progress_data = None): iArg = 1 while iArg < nArgc: - if EQUAL(args[iArg],"-f") and iArg < nArgc-1: + if EQUAL(args[iArg], "-f") and iArg < nArgc - 1: iArg = iArg + 1 pszFormat = args[iArg] - elif EQUAL(args[iArg],"-dsco") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-dsco") and iArg < nArgc - 1: iArg = iArg + 1 - papszDSCO.append(args[iArg] ) + papszDSCO.append(args[iArg]) - elif EQUAL(args[iArg],"-lco") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-lco") and iArg < nArgc - 1: iArg = iArg + 1 - papszLCO.append(args[iArg] ) + papszLCO.append(args[iArg]) - elif EQUAL(args[iArg],"-preserve_fid"): + elif EQUAL(args[iArg], "-preserve_fid"): bPreserveFID = True elif len(args[iArg]) >= 5 and EQUAL(args[iArg][0:5], "-skip"): bSkipFailures = True - nGroupTransactions = 1 # #2409 + nGroupTransactions = 1 # 2409 - elif EQUAL(args[iArg],"-append"): + elif EQUAL(args[iArg], "-append"): bAppend = True bUpdate = True - elif EQUAL(args[iArg],"-overwrite"): + elif EQUAL(args[iArg], "-overwrite"): bOverwrite = True bUpdate = True - elif EQUAL(args[iArg],"-update"): + elif EQUAL(args[iArg], "-update"): bUpdate = True - elif EQUAL(args[iArg],"-fid") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-fid") and iArg < nArgc - 1: iArg = iArg + 1 nFIDToFetch = int(args[iArg]) - elif EQUAL(args[iArg],"-sql") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-sql") and iArg < nArgc - 1: iArg = iArg + 1 pszSQLStatement = args[iArg] - elif EQUAL(args[iArg],"-nln") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-nln") and iArg < nArgc - 1: iArg = iArg + 1 pszNewLayerName = args[iArg] - elif EQUAL(args[iArg],"-nlt") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-nlt") and iArg < nArgc - 1: - if EQUAL(args[iArg+1],"NONE"): + if EQUAL(args[iArg + 1], "NONE"): eGType = ogr.wkbNone - elif EQUAL(args[iArg+1],"GEOMETRY"): + elif EQUAL(args[iArg + 1], "GEOMETRY"): eGType = ogr.wkbUnknown - elif EQUAL(args[iArg+1],"PROMOTE_TO_MULTI"): + elif EQUAL(args[iArg + 1], "PROMOTE_TO_MULTI"): bPromoteToMulti = True - elif EQUAL(args[iArg+1],"POINT"): + elif EQUAL(args[iArg + 1], "POINT"): eGType = ogr.wkbPoint - elif EQUAL(args[iArg+1],"LINESTRING"): + elif EQUAL(args[iArg + 1], "LINESTRING"): eGType = ogr.wkbLineString - elif EQUAL(args[iArg+1],"POLYGON"): + elif EQUAL(args[iArg + 1], "POLYGON"): eGType = ogr.wkbPolygon - elif EQUAL(args[iArg+1],"GEOMETRYCOLLECTION"): + elif EQUAL(args[iArg + 1], "GEOMETRYCOLLECTION"): eGType = ogr.wkbGeometryCollection - elif EQUAL(args[iArg+1],"MULTIPOINT"): + elif EQUAL(args[iArg + 1], "MULTIPOINT"): eGType = ogr.wkbMultiPoint - elif EQUAL(args[iArg+1],"MULTILINESTRING"): + elif EQUAL(args[iArg + 1], "MULTILINESTRING"): eGType = ogr.wkbMultiLineString - elif EQUAL(args[iArg+1],"MULTIPOLYGON"): + elif EQUAL(args[iArg + 1], "MULTIPOLYGON"): eGType = ogr.wkbMultiPolygon - elif EQUAL(args[iArg+1],"GEOMETRY25D"): + elif EQUAL(args[iArg + 1], "GEOMETRY25D"): eGType = ogr.wkbUnknown | ogr.wkb25DBit - elif EQUAL(args[iArg+1],"POINT25D"): + elif EQUAL(args[iArg + 1], "POINT25D"): eGType = ogr.wkbPoint25D - elif EQUAL(args[iArg+1],"LINESTRING25D"): + elif EQUAL(args[iArg + 1], "LINESTRING25D"): eGType = ogr.wkbLineString25D - elif EQUAL(args[iArg+1],"POLYGON25D"): + elif EQUAL(args[iArg + 1], "POLYGON25D"): eGType = ogr.wkbPolygon25D - elif EQUAL(args[iArg+1],"GEOMETRYCOLLECTION25D"): + elif EQUAL(args[iArg + 1], "GEOMETRYCOLLECTION25D"): eGType = ogr.wkbGeometryCollection25D - elif EQUAL(args[iArg+1],"MULTIPOINT25D"): + elif EQUAL(args[iArg + 1], "MULTIPOINT25D"): eGType = ogr.wkbMultiPoint25D - elif EQUAL(args[iArg+1],"MULTILINESTRING25D"): + elif EQUAL(args[iArg + 1], "MULTILINESTRING25D"): eGType = ogr.wkbMultiLineString25D - elif EQUAL(args[iArg+1],"MULTIPOLYGON25D"): + elif EQUAL(args[iArg + 1], "MULTIPOLYGON25D"): eGType = ogr.wkbMultiPolygon25D else: - print("-nlt %s: type not recognised." % args[iArg+1]) + print("-nlt %s: type not recognised." % args[iArg + 1]) return False iArg = iArg + 1 - elif EQUAL(args[iArg],"-dim") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-dim") and iArg < nArgc - 1: - nCoordDim = int(args[iArg+1]) + nCoordDim = int(args[iArg + 1]) if nCoordDim != 2 and nCoordDim != 3: - print("-dim %s: value not handled." % args[iArg+1]) + print("-dim %s: value not handled." % args[iArg + 1]) return False iArg = iArg + 1 - elif (EQUAL(args[iArg],"-tg") or \ - EQUAL(args[iArg],"-gt")) and iArg < nArgc-1: + elif (EQUAL(args[iArg], "-tg") or + EQUAL(args[iArg], "-gt")) and iArg < nArgc - 1: iArg = iArg + 1 nGroupTransactions = int(args[iArg]) - elif EQUAL(args[iArg],"-s_srs") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-s_srs") and iArg < nArgc - 1: iArg = iArg + 1 pszSourceSRSDef = args[iArg] - elif EQUAL(args[iArg],"-a_srs") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-a_srs") and iArg < nArgc - 1: iArg = iArg + 1 pszOutputSRSDef = args[iArg] if EQUAL(pszOutputSRSDef, "NULL") or \ @@ -312,29 +328,29 @@ def main(args = None, progress_func = TermProgress, progress_data = None): pszOutputSRSDef = None bNullifyOutputSRS = True - elif EQUAL(args[iArg],"-t_srs") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-t_srs") and iArg < nArgc - 1: iArg = iArg + 1 pszOutputSRSDef = args[iArg] bTransform = True - elif EQUAL(args[iArg],"-spat") and iArg + 4 < nArgc: + elif EQUAL(args[iArg], "-spat") and iArg + 4 < nArgc: oRing = ogr.Geometry(ogr.wkbLinearRing) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) poSpatialFilter = ogr.Geometry(ogr.wkbPolygon) poSpatialFilter.AddGeometry(oRing) iArg = iArg + 4 - elif EQUAL(args[iArg],"-where") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-where") and iArg < nArgc - 1: iArg = iArg + 1 pszWHERE = args[iArg] - elif EQUAL(args[iArg],"-select") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-select") and iArg < nArgc - 1: iArg = iArg + 1 pszSelect = args[iArg] if pszSelect.find(',') != -1: @@ -344,17 +360,17 @@ def main(args = None, progress_func = TermProgress, progress_data = None): if papszSelFields[0] == '': papszSelFields = [] - elif EQUAL(args[iArg],"-simplify") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-simplify") and iArg < nArgc - 1: iArg = iArg + 1 eGeomOp = GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY dfGeomOpParam = float(args[iArg]) - elif EQUAL(args[iArg],"-segmentize") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-segmentize") and iArg < nArgc - 1: iArg = iArg + 1 eGeomOp = GeomOperation.SEGMENTIZE dfGeomOpParam = float(args[iArg]) - elif EQUAL(args[iArg],"-fieldTypeToString") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-fieldTypeToString") and iArg < nArgc - 1: iArg = iArg + 1 pszFieldTypeToString = args[iArg] if pszFieldTypeToString.find(',') != -1: @@ -363,127 +379,127 @@ def main(args = None, progress_func = TermProgress, progress_data = None): tokens = pszFieldTypeToString.split(' ') for token in tokens: - if EQUAL(token,"Integer") or \ - EQUAL(token,"Real") or \ - EQUAL(token,"String") or \ - EQUAL(token,"Date") or \ - EQUAL(token,"Time") or \ - EQUAL(token,"DateTime") or \ - EQUAL(token,"Binary") or \ - EQUAL(token,"IntegerList") or \ - EQUAL(token,"RealList") or \ - EQUAL(token,"StringList"): + if EQUAL(token, "Integer") or \ + EQUAL(token, "Real") or \ + EQUAL(token, "String") or \ + EQUAL(token, "Date") or \ + EQUAL(token, "Time") or \ + EQUAL(token, "DateTime") or \ + EQUAL(token, "Binary") or \ + EQUAL(token, "IntegerList") or \ + EQUAL(token, "RealList") or \ + EQUAL(token, "StringList"): papszFieldTypesToString.append(token) - elif EQUAL(token,"All"): - papszFieldTypesToString = [ 'All' ] + elif EQUAL(token, "All"): + papszFieldTypesToString = ['All'] break else: print("Unhandled type for fieldtypeasstring option : %s " % token) return Usage() - elif EQUAL(args[iArg],"-progress"): + elif EQUAL(args[iArg], "-progress"): bDisplayProgress = True - #elif EQUAL(args[iArg],"-wrapdateline") ) - #{ + # elif EQUAL(args[iArg],"-wrapdateline") ) + # { # bWrapDateline = True; - #} + # } # - elif EQUAL(args[iArg],"-clipsrc") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-clipsrc") and iArg < nArgc - 1: bClipSrc = True - if IsNumber(args[iArg+1]) and iArg < nArgc - 4: + if IsNumber(args[iArg + 1]) and iArg < nArgc - 4: oRing = ogr.Geometry(ogr.wkbLinearRing) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) poClipSrc = ogr.Geometry(ogr.wkbPolygon) poClipSrc.AddGeometry(oRing) iArg = iArg + 4 - elif (len(args[iArg+1]) >= 7 and EQUAL(args[iArg+1][0:7],"POLYGON") ) or \ - (len(args[iArg+1]) >= 12 and EQUAL(args[iArg+1][0:12],"MULTIPOLYGON") ) : - poClipSrc = ogr.CreateGeometryFromWkt(args[iArg+1]) + elif (len(args[iArg + 1]) >= 7 and EQUAL(args[iArg + 1][0:7], "POLYGON")) or \ + (len(args[iArg + 1]) >= 12 and EQUAL(args[iArg + 1][0:12], "MULTIPOLYGON")): + poClipSrc = ogr.CreateGeometryFromWkt(args[iArg + 1]) if poClipSrc is None: print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n") return Usage() iArg = iArg + 1 - elif EQUAL(args[iArg+1],"spat_extent"): + elif EQUAL(args[iArg + 1], "spat_extent"): iArg = iArg + 1 else: - pszClipSrcDS = args[iArg+1] + pszClipSrcDS = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipsrcsql") and iArg < nArgc-1: - pszClipSrcSQL = args[iArg+1] + elif EQUAL(args[iArg], "-clipsrcsql") and iArg < nArgc - 1: + pszClipSrcSQL = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipsrclayer") and iArg < nArgc-1: - pszClipSrcLayer = args[iArg+1] + elif EQUAL(args[iArg], "-clipsrclayer") and iArg < nArgc - 1: + pszClipSrcLayer = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipsrcwhere") and iArg < nArgc-1: - pszClipSrcWhere = args[iArg+1] + elif EQUAL(args[iArg], "-clipsrcwhere") and iArg < nArgc - 1: + pszClipSrcWhere = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipdst") and iArg < nArgc-1: + elif EQUAL(args[iArg], "-clipdst") and iArg < nArgc - 1: - if IsNumber(args[iArg+1]) and iArg < nArgc - 4: + if IsNumber(args[iArg + 1]) and iArg < nArgc - 4: oRing = ogr.Geometry(ogr.wkbLinearRing) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+4]) ) - oRing.AddPoint_2D( float(args[iArg+3]), float(args[iArg+2]) ) - oRing.AddPoint_2D( float(args[iArg+1]), float(args[iArg+2]) ) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) + oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) + oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) poClipDst = ogr.Geometry(ogr.wkbPolygon) poClipDst.AddGeometry(oRing) iArg = iArg + 4 - elif (len(args[iArg+1]) >= 7 and EQUAL(args[iArg+1][0:7],"POLYGON") ) or \ - (len(args[iArg+1]) >= 12 and EQUAL(args[iArg+1][0:12],"MULTIPOLYGON") ) : - poClipDst = ogr.CreateGeometryFromWkt(args[iArg+1]) + elif (len(args[iArg + 1]) >= 7 and EQUAL(args[iArg + 1][0:7], "POLYGON")) or \ + (len(args[iArg + 1]) >= 12 and EQUAL(args[iArg + 1][0:12], "MULTIPOLYGON")): + poClipDst = ogr.CreateGeometryFromWkt(args[iArg + 1]) if poClipDst is None: print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n") return Usage() iArg = iArg + 1 - elif EQUAL(args[iArg+1],"spat_extent"): + elif EQUAL(args[iArg + 1], "spat_extent"): iArg = iArg + 1 else: - pszClipDstDS = args[iArg+1] + pszClipDstDS = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipdstsql") and iArg < nArgc-1: - pszClipDstSQL = args[iArg+1] + elif EQUAL(args[iArg], "-clipdstsql") and iArg < nArgc - 1: + pszClipDstSQL = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipdstlayer") and iArg < nArgc-1: - pszClipDstLayer = args[iArg+1] + elif EQUAL(args[iArg], "-clipdstlayer") and iArg < nArgc - 1: + pszClipDstLayer = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-clipdstwhere") and iArg < nArgc-1: - pszClipDstWhere = args[iArg+1] + elif EQUAL(args[iArg], "-clipdstwhere") and iArg < nArgc - 1: + pszClipDstWhere = args[iArg + 1] iArg = iArg + 1 - elif EQUAL(args[iArg],"-explodecollections"): + elif EQUAL(args[iArg], "-explodecollections"): bExplodeCollections = True - elif EQUAL(args[iArg],"-zfield") and iArg < nArgc-1: - pszZField = args[iArg+1] + elif EQUAL(args[iArg], "-zfield") and iArg < nArgc - 1: + pszZField = args[iArg + 1] iArg = iArg + 1 elif args[iArg][0] == '-': @@ -494,7 +510,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): elif pszDataSource is None: pszDataSource = args[iArg] else: - papszLayers.append (args[iArg] ) + papszLayers.append(args[iArg]) iArg = iArg + 1 @@ -508,37 +524,37 @@ def main(args = None, progress_func = TermProgress, progress_data = None): if bClipSrc and pszClipSrcDS is not None: poClipSrc = LoadGeometry(pszClipSrcDS, pszClipSrcSQL, pszClipSrcLayer, pszClipSrcWhere) if poClipSrc is None: - print("FAILURE: cannot load source clip geometry\n" ) + print("FAILURE: cannot load source clip geometry\n") return Usage() elif bClipSrc and poClipSrc is None: if poSpatialFilter is not None: poClipSrc = poSpatialFilter.Clone() if poClipSrc is None: - print("FAILURE: -clipsrc must be used with -spat option or a\n" + \ + print("FAILURE: -clipsrc must be used with -spat option or a\n" + "bounding box, WKT string or datasource must be specified\n") return Usage() if pszClipDstDS is not None: poClipDst = LoadGeometry(pszClipDstDS, pszClipDstSQL, pszClipDstLayer, pszClipDstWhere) if poClipDst is None: - print("FAILURE: cannot load dest clip geometry\n" ) + print("FAILURE: cannot load dest clip geometry\n") return Usage() # -------------------------------------------------------------------- # Open data source. # -------------------------------------------------------------------- - poDS = ogr.Open( pszDataSource, False ) + poDS = ogr.Open(pszDataSource, False) # -------------------------------------------------------------------- # Report failure # -------------------------------------------------------------------- if poDS is None: - print("FAILURE:\n" + \ - "Unable to open datasource `%s' with the following drivers." % pszDataSource) + print("FAILURE:\n" + + "Unable to open datasource `%s' with the following drivers." % pszDataSource) for iDriver in range(ogr.GetDriverCount()): - print(" -> " + ogr.GetDriver(iDriver).GetName() ) + print(" -> " + ogr.GetDriver(iDriver).GetName()) return False @@ -549,11 +565,11 @@ def main(args = None, progress_func = TermProgress, progress_data = None): poDriver = None if bUpdate: - poODS = ogr.Open( pszDestDataSource, True ) + poODS = ogr.Open(pszDestDataSource, True) if poODS is None: if bOverwrite or bAppend: - poODS = ogr.Open( pszDestDataSource, False ) + poODS = ogr.Open(pszDestDataSource, False) if poODS is None: # the datasource doesn't exist at all bUpdate = False @@ -563,12 +579,12 @@ def main(args = None, progress_func = TermProgress, progress_data = None): if bUpdate: print("FAILURE:\n" + - "Unable to open existing output datasource `%s'." % pszDestDataSource) + "Unable to open existing output datasource `%s'." % pszDestDataSource) return False - elif len(papszDSCO) > 0: - print("WARNING: Datasource creation options ignored since an existing datasource\n" + \ - " being updated." ) + elif papszDSCO: + print("WARNING: Datasource creation options ignored since an existing datasource\n" + + " being updated.") if poODS is not None: poDriver = poODS.GetDriver() @@ -580,15 +596,15 @@ def main(args = None, progress_func = TermProgress, progress_data = None): poDriver = ogr.GetDriverByName(pszFormat) if poDriver is None: print("Unable to find driver `%s'." % pszFormat) - print( "The following drivers are available:" ) + print("The following drivers are available:") for iDriver in range(ogr.GetDriverCount()): - print(" -> %s" % ogr.GetDriver(iDriver).GetName() ) + print(" -> %s" % ogr.GetDriver(iDriver).GetName()) return False - if poDriver.TestCapability( ogr.ODrCCreateDataSource ) == False: - print( "%s driver does not support data source creation." % pszFormat) + if not poDriver.TestCapability(ogr.ODrCCreateDataSource): + print("%s driver does not support data source creation." % pszFormat) return False # -------------------------------------------------------------------- @@ -601,29 +617,29 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- if EQUAL(poDriver.GetName(), "ESRI Shapefile") and \ pszSQLStatement is None and \ - (len(papszLayers) > 1 or \ - (len(papszLayers) == 0 and poDS.GetLayerCount() > 1)) and \ - pszNewLayerName is None and \ - EQUAL(os.path.splitext(pszDestDataSource)[1], ".SHP") : + (len(papszLayers) > 1 or + (not papszLayers and poDS.GetLayerCount() > 1)) and \ + pszNewLayerName is None and \ + EQUAL(os.path.splitext(pszDestDataSource)[1], ".SHP"): try: os.stat(pszDestDataSource) - except: + except OSError: try: # decimal 493 = octal 0755. Python 3 needs 0o755, but # this syntax is only supported by Python >= 2.6 os.mkdir(pszDestDataSource, 493) - except: + except OSError: print("Failed to create directory %s\n" - "for shapefile datastore.\n" % pszDestDataSource ) + "for shapefile datastore.\n" % pszDestDataSource) return False # -------------------------------------------------------------------- # Create the output data source. # -------------------------------------------------------------------- - poODS = poDriver.CreateDataSource( pszDestDataSource, options = papszDSCO ) + poODS = poDriver.CreateDataSource(pszDestDataSource, options=papszDSCO) if poODS is None: - print( "%s driver failed to create %s" % (pszFormat, pszDestDataSource )) + print("%s driver failed to create %s" % (pszFormat, pszDestDataSource)) return False # -------------------------------------------------------------------- @@ -631,8 +647,9 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- if pszOutputSRSDef is not None: poOutputSRS = osr.SpatialReference() - if poOutputSRS.SetFromUserInput( pszOutputSRSDef ) != 0: - print( "Failed to process SRS definition: %s" % pszOutputSRSDef ) + poOutputSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) + if poOutputSRS.SetFromUserInput(pszOutputSRSDef) != 0: + print("Failed to process SRS definition: %s" % pszOutputSRSDef) return False # -------------------------------------------------------------------- @@ -640,15 +657,16 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- if pszSourceSRSDef is not None: poSourceSRS = osr.SpatialReference() - if poSourceSRS.SetFromUserInput( pszSourceSRSDef ) != 0: - print( "Failed to process SRS definition: %s" % pszSourceSRSDef ) + poSourceSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) + if poSourceSRS.SetFromUserInput(pszSourceSRSDef) != 0: + print("Failed to process SRS definition: %s" % pszSourceSRSDef) return False # -------------------------------------------------------------------- # For OSM file. # -------------------------------------------------------------------- bSrcIsOSM = poDS.GetDriver() is not None and \ - poDS.GetDriver().GetName() == "OSM" + poDS.GetDriver().GetName() == "OSM" nSrcFileSize = 0 if bSrcIsOSM and poDS.GetName() != "/vsistdin/": sStat = gdal.VSIStatL(poDS.GetName()) @@ -660,12 +678,12 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- if pszSQLStatement is not None: if pszWHERE is not None: - print( "-where clause ignored in combination with -sql." ) - if len(papszLayers) > 0: - print( "layer names ignored in combination with -sql." ) + print("-where clause ignored in combination with -sql.") + if papszLayers: + print("layer names ignored in combination with -sql.") - poResultSet = poDS.ExecuteSQL( pszSQLStatement, poSpatialFilter, \ - None ) + poResultSet = poDS.ExecuteSQL(pszSQLStatement, poSpatialFilter, + None) if poResultSet is not None: nCountLayerFeatures = 0 @@ -675,7 +693,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): pProgressArg = progress_data elif not poResultSet.TestCapability(ogr.OLCFastFeatureCount): - print( "Progress turned off as fast feature count is not available.") + print("Progress turned off as fast feature count is not available.") bDisplayProgress = False else: @@ -689,50 +707,49 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # the layer name isn't specified # -------------------------------------------------------------------- if EQUAL(poDriver.GetName(), "ESRI Shapefile") and \ - pszNewLayerName is None: + pszNewLayerName is None: try: mode = os.stat(pszDestDataSource).st_mode if (mode & stat.S_IFDIR) == 0: pszNewLayerName = os.path.splitext(os.path.basename(pszDestDataSource))[0] - except: + except OSError: pass - - psInfo = SetupTargetLayer( poDS, \ - poResultSet, - poODS, \ - papszLCO, \ - pszNewLayerName, \ - bTransform, \ - poOutputSRS, \ - bNullifyOutputSRS, \ - poSourceSRS, \ - papszSelFields, \ - bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, \ - papszFieldTypesToString, \ - bWrapDateline, \ - bExplodeCollections, \ - pszZField, \ - pszWHERE ) + psInfo = SetupTargetLayer(poDS, + poResultSet, + poODS, + papszLCO, + pszNewLayerName, + bTransform, + poOutputSRS, + bNullifyOutputSRS, + poSourceSRS, + papszSelFields, + bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, + papszFieldTypesToString, + bWrapDateline, + bExplodeCollections, + pszZField, + pszWHERE) poResultSet.ResetReading() - if psInfo is None or not TranslateLayer( psInfo, poDS, poResultSet, poODS, \ - poOutputSRS, bNullifyOutputSRS, \ - eGType, bPromoteToMulti, nCoordDim, \ - eGeomOp, dfGeomOpParam, \ - nCountLayerFeatures, \ - poClipSrc, poClipDst, \ - bExplodeCollections, \ - nSrcFileSize, None, \ - pfnProgress, pProgressArg ): + if psInfo is None or not TranslateLayer(psInfo, poDS, poResultSet, poODS, + poOutputSRS, bNullifyOutputSRS, + eGType, bPromoteToMulti, nCoordDim, + eGeomOp, dfGeomOpParam, + nCountLayerFeatures, + poClipSrc, poClipDst, + bExplodeCollections, + nSrcFileSize, None, + pfnProgress, pProgressArg): print( - "Terminating translation prematurely after failed\n" + \ - "translation from sql statement." ) + "Terminating translation prematurely after failed\n" + + "translation from sql statement.") return False - poDS.ReleaseResultSet( poResultSet ) + poDS.ReleaseResultSet(poResultSet) # -------------------------------------------------------------------- @@ -742,14 +759,14 @@ def main(args = None, progress_func = TermProgress, progress_data = None): gdal.SetConfigOption("OGR_INTERLEAVED_READING", "YES") - #if (bSplitListFields) - #{ + # if (bSplitListFields) + # { # fprintf( stderr, "FAILURE: -splitlistfields not supported in this mode\n" ); # exit( 1 ); - #} + # } nSrcLayerCount = poDS.GetLayerCount() - pasAssocLayers = [ AssociatedLayers() for i in range(nSrcLayerCount) ] + pasAssocLayers = [AssociatedLayers() for _ in range(nSrcLayerCount)] # -------------------------------------------------------------------- # Special case to improve user experience when translating into @@ -763,7 +780,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): mode = os.stat(pszDestDataSource).st_mode if (mode & stat.S_IFDIR) == 0: pszNewLayerName = os.path.splitext(os.path.basename(pszDestDataSource))[0] - except: + except OSError: pass if bDisplayProgress and bSrcIsOSM: @@ -773,8 +790,8 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- # If no target layer specified, use all source layers. # -------------------------------------------------------------------- - if len(papszLayers) == 0: - papszLayers = [ None for i in range(nSrcLayerCount) ] + if not papszLayers: + papszLayers = [None] * nSrcLayerCount for iLayer in range(nSrcLayerCount): poLayer = poDS.GetLayer(iLayer) if poLayer is None: @@ -785,10 +802,10 @@ def main(args = None, progress_func = TermProgress, progress_data = None): else: if bSrcIsOSM: osInterestLayers = "SET interest_layers =" - for iLayer in range(len(papszLayers)): + for iLayer, papszLayer in enumerate(papszLayers): if iLayer != 0: osInterestLayers = osInterestLayers + "," - osInterestLayers = osInterestLayers + papszLayers[iLayer] + osInterestLayers = osInterestLayers + papszLayer poDS.ExecuteSQL(osInterestLayers, None, None) @@ -805,30 +822,30 @@ def main(args = None, progress_func = TermProgress, progress_data = None): if CSLFindString(papszLayers, poLayer.GetName()) >= 0: if pszWHERE is not None: - if poLayer.SetAttributeFilter( pszWHERE ) != 0: - print("FAILURE: SetAttributeFilter(%s) on layer '%s' failed.\n" % (pszWHERE, poLayer.GetName()) ) + if poLayer.SetAttributeFilter(pszWHERE) != 0: + print("FAILURE: SetAttributeFilter(%s) on layer '%s' failed.\n" % (pszWHERE, poLayer.GetName())) if not bSkipFailures: return False if poSpatialFilter is not None: - poLayer.SetSpatialFilter( poSpatialFilter ) - - psInfo = SetupTargetLayer( poDS, \ - poLayer, \ - poODS, \ - papszLCO, \ - pszNewLayerName, \ - bTransform, \ - poOutputSRS, \ - bNullifyOutputSRS, \ - poSourceSRS, \ - papszSelFields, \ - bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, \ - papszFieldTypesToString, \ - bWrapDateline, \ - bExplodeCollections, \ - pszZField, \ - pszWHERE ) + poLayer.SetSpatialFilter(poSpatialFilter) + + psInfo = SetupTargetLayer(poDS, + poLayer, + poODS, + papszLCO, + pszNewLayerName, + bTransform, + poOutputSRS, + bNullifyOutputSRS, + poSourceSRS, + papszSelFields, + bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, + papszFieldTypesToString, + bWrapDateline, + bExplodeCollections, + pszZField, + pszWHERE) if psInfo is None and not bSkipFailures: return False @@ -850,20 +867,20 @@ def main(args = None, progress_func = TermProgress, progress_data = None): anReadFeatureCount = [0] if psInfo is not None: - if not TranslateLayer(psInfo, poDS, poLayer, poODS, \ - poOutputSRS, bNullifyOutputSRS, \ - eGType, bPromoteToMulti, nCoordDim, \ - eGeomOp, dfGeomOpParam, \ - 0, \ - poClipSrc, poClipDst, \ - bExplodeCollections, \ - nSrcFileSize, \ - anReadFeatureCount, \ - pfnProgress, pProgressArg ) \ - and not bSkipFailures: + if not TranslateLayer(psInfo, poDS, poLayer, poODS, + poOutputSRS, bNullifyOutputSRS, + eGType, bPromoteToMulti, nCoordDim, + eGeomOp, dfGeomOpParam, + 0, + poClipSrc, poClipDst, + bExplodeCollections, + nSrcFileSize, + anReadFeatureCount, + pfnProgress, pProgressArg) \ + and not bSkipFailures: print( - "Terminating translation prematurely after failed\n" + \ - "translation of layer " + poLayer.GetName() + " (use -skipfailures to skip errors)") + "Terminating translation prematurely after failed\n" + + "translation of layer " + poLayer.GetName() + " (use -skipfailures to skip errors)") return False else: @@ -885,9 +902,9 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- # Process each data source layer. # -------------------------------------------------------------------- - if len(papszLayers) == 0: + if not papszLayers: nLayerCount = poDS.GetLayerCount() - papoLayers = [None for i in range(nLayerCount)] + papoLayers = [None] * nLayerCount iLayer = 0 for iLayer in range(nLayerCount): @@ -905,7 +922,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # -------------------------------------------------------------------- else: nLayerCount = len(papszLayers) - papoLayers = [None for i in range(nLayerCount)] + papoLayers = [None] * nLayerCount iLayer = 0 for layername in papszLayers: @@ -918,7 +935,7 @@ def main(args = None, progress_func = TermProgress, progress_data = None): papoLayers[iLayer] = poLayer iLayer = iLayer + 1 - panLayerCountFeatures = [0 for i in range(nLayerCount)] + panLayerCountFeatures = [0] * nLayerCount nCountLayersFeatures = 0 nAccCountFeatures = 0 @@ -927,13 +944,13 @@ def main(args = None, progress_func = TermProgress, progress_data = None): poLayer = papoLayers[iLayer] if pszWHERE is not None: - if poLayer.SetAttributeFilter( pszWHERE ) != 0: + if poLayer.SetAttributeFilter(pszWHERE) != 0: print("FAILURE: SetAttributeFilter(%s) failed." % pszWHERE) if not bSkipFailures: return False if poSpatialFilter is not None: - poLayer.SetSpatialFilter( poSpatialFilter ) + poLayer.SetSpatialFilter(poSpatialFilter) if bDisplayProgress and not bSrcIsOSM: if not poLayer.TestCapability(ogr.OLCFastFeatureCount): @@ -953,10 +970,10 @@ def main(args = None, progress_func = TermProgress, progress_data = None): pProgressArg = progress_data else: pfnProgress = ScaledProgressFunc - pProgressArg = ScaledProgressObject( \ - nAccCountFeatures * 1.0 / nCountLayersFeatures, \ - (nAccCountFeatures + panLayerCountFeatures[iLayer]) * 1.0 / nCountLayersFeatures, \ - progress_func, progress_data) + pProgressArg = ScaledProgressObject( + nAccCountFeatures * 1.0 / nCountLayersFeatures, + (nAccCountFeatures + panLayerCountFeatures[iLayer]) * 1.0 / nCountLayersFeatures, + progress_func, progress_data) nAccCountFeatures += panLayerCountFeatures[iLayer] @@ -966,48 +983,47 @@ def main(args = None, progress_func = TermProgress, progress_data = None): # the layer name isn't specified # -------------------------------------------------------------------- if EQUAL(poDriver.GetName(), "ESRI Shapefile") and \ - nLayerCount == 1 and pszNewLayerName is None: + nLayerCount == 1 and pszNewLayerName is None: try: mode = os.stat(pszDestDataSource).st_mode if (mode & stat.S_IFDIR) == 0: pszNewLayerName = os.path.splitext(os.path.basename(pszDestDataSource))[0] - except: + except OSError: pass - - psInfo = SetupTargetLayer( poDS, \ - poLayer, \ - poODS, \ - papszLCO, \ - pszNewLayerName, \ - bTransform, \ - poOutputSRS, \ - bNullifyOutputSRS, \ - poSourceSRS, \ - papszSelFields, \ - bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, \ - papszFieldTypesToString, \ - bWrapDateline, \ - bExplodeCollections, \ - pszZField, \ - pszWHERE ) + psInfo = SetupTargetLayer(poDS, + poLayer, + poODS, + papszLCO, + pszNewLayerName, + bTransform, + poOutputSRS, + bNullifyOutputSRS, + poSourceSRS, + papszSelFields, + bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, + papszFieldTypesToString, + bWrapDateline, + bExplodeCollections, + pszZField, + pszWHERE) poLayer.ResetReading() - if (psInfo is None or \ - not TranslateLayer( psInfo, poDS, poLayer, poODS, \ - poOutputSRS, bNullifyOutputSRS, \ - eGType, bPromoteToMulti, nCoordDim, \ - eGeomOp, dfGeomOpParam, \ - panLayerCountFeatures[iLayer], \ - poClipSrc, poClipDst, \ - bExplodeCollections, \ - nSrcFileSize, None, \ - pfnProgress, pProgressArg )) \ - and not bSkipFailures: + if (psInfo is None or + not TranslateLayer(psInfo, poDS, poLayer, poODS, + poOutputSRS, bNullifyOutputSRS, + eGType, bPromoteToMulti, nCoordDim, + eGeomOp, dfGeomOpParam, + panLayerCountFeatures[iLayer], + poClipSrc, poClipDst, + bExplodeCollections, + nSrcFileSize, None, + pfnProgress, pProgressArg)) \ + and not bSkipFailures: print( - "Terminating translation prematurely after failed\n" + \ - "translation of layer " + poLayer.GetLayerDefn().GetName() + " (use -skipfailures to skip errors)") + "Terminating translation prematurely after failed\n" + + "translation of layer " + poLayer.GetLayerDefn().GetName() + " (use -skipfailures to skip errors)") return False @@ -1021,69 +1037,71 @@ def main(args = None, progress_func = TermProgress, progress_data = None): return True -#********************************************************************** +# ********************************************************************** # Usage() -#********************************************************************** +# ********************************************************************** + def Usage(): - print( "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n" + \ - " [-select field_list] [-where restricted_where] \n" + \ - " [-progress] [-sql ] \n" + \ - " [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n" + \ - " [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n" + \ - " [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n" + \ - " [-simplify tolerance]\n" + \ - #// " [-segmentize max_dist] [-fieldTypeToString All|(type1[,type2]*)]\n" + \ - " [-fieldTypeToString All|(type1[,type2]*)] [-explodecollections] \n" + \ - " dst_datasource_name src_datasource_name\n" + \ - " [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3] [layer [layer ...]]\n" + \ - "\n" + \ - " -f format_name: output file format name, possible values are:") + print("Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n" + + " [-select field_list] [-where restricted_where] \n" + + " [-progress] [-sql ] \n" + + " [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n" + + " [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n" + + " [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n" + + " [-simplify tolerance]\n" + \ + # // " [-segmentize max_dist] [-fieldTypeToString All|(type1[,type2]*)]\n" + \ + " [-fieldTypeToString All|(type1[,type2]*)] [-explodecollections] \n" + \ + " dst_datasource_name src_datasource_name\n" + \ + " [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3] [layer [layer ...]]\n" + \ + "\n" + \ + " -f format_name: output file format name, possible values are:") for iDriver in range(ogr.GetDriverCount()): poDriver = ogr.GetDriver(iDriver) - if poDriver.TestCapability( ogr.ODrCCreateDataSource ): - print( " -f \"" + poDriver.GetName() + "\"" ) - - print( " -append: Append to existing layer instead of creating new if it exists\n" + \ - " -overwrite: delete the output layer and recreate it empty\n" + \ - " -update: Open existing output datasource in update mode\n" + \ - " -progress: Display progress on terminal. Only works if input layers have the \"fast feature count\" capability\n" + \ - " -select field_list: Comma-delimited list of fields from input layer to\n" + \ - " copy to the new layer (defaults to all)\n" + \ - " -where restricted_where: Attribute query (like SQL WHERE)\n" + \ - " -sql statement: Execute given SQL statement and save result.\n" + \ - " -skipfailures: skip features or layers that fail to convert\n" + \ - " -gt n: group n features per transaction (default 200)\n" + \ - " -spat xmin ymin xmax ymax: spatial query extents\n" + \ - " -simplify tolerance: distance tolerance for simplification.\n" + \ - #//" -segmentize max_dist: maximum distance between 2 nodes.\n" + \ - #//" Used to create intermediate points\n" + \ - " -dsco NAME=VALUE: Dataset creation option (format specific)\n" + \ - " -lco NAME=VALUE: Layer creation option (format specific)\n" + \ - " -nln name: Assign an alternate name to the new layer\n" + \ - " -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY,\n" + \ - " POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n" + \ - " MULTIPOLYGON, or MULTILINESTRING. Add \"25D\" for 3D layers.\n" + \ - " Default is type of source layer.\n" + \ - " -dim dimension: Force the coordinate dimension to the specified value.\n" + \ - " -fieldTypeToString type1,...: Converts fields of specified types to\n" + \ - " fields of type string in the new layer. Valid types are : \n" + \ - " Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,\n" + \ - " StringList. Special value All can be used to convert all fields to strings.") + if poDriver.TestCapability(ogr.ODrCCreateDataSource): + print(" -f \"" + poDriver.GetName() + "\"") + + print(" -append: Append to existing layer instead of creating new if it exists\n" + + " -overwrite: delete the output layer and recreate it empty\n" + + " -update: Open existing output datasource in update mode\n" + + " -progress: Display progress on terminal. Only works if input layers have the \"fast feature count\" capability\n" + + " -select field_list: Comma-delimited list of fields from input layer to\n" + + " copy to the new layer (defaults to all)\n" + + " -where restricted_where: Attribute query (like SQL WHERE)\n" + + " -sql statement: Execute given SQL statement and save result.\n" + + " -skipfailures: skip features or layers that fail to convert\n" + + " -gt n: group n features per transaction (default 200)\n" + + " -spat xmin ymin xmax ymax: spatial query extents\n" + + " -simplify tolerance: distance tolerance for simplification.\n" + \ + # //" -segmentize max_dist: maximum distance between 2 nodes.\n" + \ + # //" Used to create intermediate points\n" + \ + " -dsco NAME=VALUE: Dataset creation option (format specific)\n" + \ + " -lco NAME=VALUE: Layer creation option (format specific)\n" + \ + " -nln name: Assign an alternate name to the new layer\n" + \ + " -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY,\n" + \ + " POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n" + \ + " MULTIPOLYGON, or MULTILINESTRING. Add \"25D\" for 3D layers.\n" + \ + " Default is type of source layer.\n" + \ + " -dim dimension: Force the coordinate dimension to the specified value.\n" + \ + " -fieldTypeToString type1,...: Converts fields of specified types to\n" + \ + " fields of type string in the new layer. Valid types are : \n" + \ + " Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,\n" + \ + " StringList. Special value All can be used to convert all fields to strings.") print(" -a_srs srs_def: Assign an output SRS\n" - " -t_srs srs_def: Reproject/transform to this SRS on output\n" - " -s_srs srs_def: Override source SRS\n" - "\n" - " Srs_def can be a full WKT definition (hard to escape properly),\n" - " or a well known definition (i.e. EPSG:4326) or a file with a WKT\n" - " definition." ) + " -t_srs srs_def: Reproject/transform to this SRS on output\n" + " -s_srs srs_def: Override source SRS\n" + "\n" + " Srs_def can be a full WKT definition (hard to escape properly),\n" + " or a well known definition (i.e. EPSG:4326) or a file with a WKT\n" + " definition.") return False + def CSLFindString(v, mystr): i = 0 for strIter in v: @@ -1092,22 +1110,24 @@ def CSLFindString(v, mystr): i = i + 1 return -1 -def IsNumber( pszStr): + +def IsNumber(pszStr): try: - (float)(pszStr) + float(pszStr) return True - except: + except ValueError: return False -def LoadGeometry( pszDS, pszSQL, pszLyr, pszWhere): + +def LoadGeometry(pszDS, pszSQL, pszLyr, pszWhere): poGeom = None - poDS = ogr.Open( pszDS, False ) + poDS = ogr.Open(pszDS, False) if poDS is None: return None if pszSQL is not None: - poLyr = poDS.ExecuteSQL( pszSQL, None, None ) + poLyr = poDS.ExecuteSQL(pszSQL, None, None) elif pszLyr is not None: poLyr = poDS.GetLayerByName(pszLyr) else: @@ -1128,25 +1148,25 @@ def LoadGeometry( pszDS, pszSQL, pszLyr, pszWhere): eType = wkbFlatten(poSrcGeom.GetGeometryType()) if poGeom is None: - poGeom = ogr.Geometry( ogr.wkbMultiPolygon ) + poGeom = ogr.Geometry(ogr.wkbMultiPolygon) if eType == ogr.wkbPolygon: - poGeom.AddGeometry( poSrcGeom ) + poGeom.AddGeometry(poSrcGeom) elif eType == ogr.wkbMultiPolygon: for iGeom in range(poSrcGeom.GetGeometryCount()): - poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom) ) + poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom)) else: - print("ERROR: Geometry not of polygon type." ) + print("ERROR: Geometry not of polygon type.") if pszSQL is not None: - poDS.ReleaseResultSet( poLyr ) + poDS.ReleaseResultSet(poLyr) poDS.Destroy() return None poFeat = poLyr.GetNextFeature() if pszSQL is not None: - poDS.ReleaseResultSet( poLyr ) + poDS.ReleaseResultSet(poLyr) poDS.Destroy() return poGeom @@ -1155,11 +1175,12 @@ def LoadGeometry( pszDS, pszSQL, pszLyr, pszWhere): def wkbFlatten(x): return x & (~ogr.wkb25DBit) -#********************************************************************** +# ********************************************************************** # SetZ() -#********************************************************************** +# ********************************************************************** -def SetZ (poGeom, dfZ ): + +def SetZ(poGeom, dfZ): if poGeom is None: return @@ -1169,28 +1190,29 @@ def SetZ (poGeom, dfZ ): poGeom.SetPoint(0, poGeom.GetX(), poGeom.GetY(), dfZ) elif eGType == ogr.wkbLineString or \ - eGType == ogr.wkbLinearRing: + eGType == ogr.wkbLinearRing: for i in range(poGeom.GetPointCount()): poGeom.SetPoint(i, poGeom.GetX(i), poGeom.GetY(i), dfZ) elif eGType == ogr.wkbPolygon or \ - eGType == ogr.wkbMultiPoint or \ - eGType == ogr.wkbMultiLineString or \ - eGType == ogr.wkbMultiPolygon or \ - eGType == ogr.wkbGeometryCollection: + eGType == ogr.wkbMultiPoint or \ + eGType == ogr.wkbMultiLineString or \ + eGType == ogr.wkbMultiPolygon or \ + eGType == ogr.wkbGeometryCollection: for i in range(poGeom.GetGeometryCount()): SetZ(poGeom.GetGeometryRef(i), dfZ) -#********************************************************************** +# ********************************************************************** # SetupTargetLayer() -#********************************************************************** +# ********************************************************************** -def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ - bTransform, poOutputSRS, bNullifyOutputSRS, poSourceSRS, papszSelFields, \ - bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, \ - papszFieldTypesToString, bWrapDateline, \ - bExplodeCollections, pszZField, pszWHERE) : +def SetupTargetLayer(poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, + bTransform, poOutputSRS, bNullifyOutputSRS, poSourceSRS, papszSelFields, + bAppend, eGType, bPromoteToMulti, nCoordDim, bOverwrite, + papszFieldTypesToString, bWrapDateline, + bExplodeCollections, pszZField, pszWHERE): + # pylint: disable=unused-argument if pszNewLayerName is None: pszNewLayerName = poSrcLayer.GetLayerDefn().GetName() @@ -1204,27 +1226,23 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ poSourceSRS = poSrcLayer.GetSpatialRef() if poSourceSRS is None: - print("Can't transform coordinates, source layer has no\n" + \ - "coordinate system. Use -s_srs to set one." ) + print("Can't transform coordinates, source layer has no\n" + + "coordinate system. Use -s_srs to set one.") return None - poCT = osr.CoordinateTransformation( poSourceSRS, poOutputSRS ) - if gdal.GetLastErrorMsg().find( 'Unable to load PROJ.4 library' ) != -1: - poCT = None - + poCT = osr.CoordinateTransformation(poSourceSRS, poOutputSRS) if poCT is None: pszWKT = None - print("Failed to create coordinate transformation between the\n" + \ - "following coordinate systems. This may be because they\n" + \ - "are not transformable, or because projection services\n" + \ - "(PROJ.4 DLL/.so) could not be loaded." ) + print("Failed to create coordinate transformation between the\n" + + "following coordinate systems. This may be because they\n" + + "are not transformable.") - pszWKT = poSourceSRS.ExportToPrettyWkt( 0 ) - print( "Source:\n" + pszWKT ) + pszWKT = poSourceSRS.ExportToPrettyWkt(0) + print("Source:\n" + pszWKT) - pszWKT = poOutputSRS.ExportToPrettyWkt( 0 ) - print( "Target:\n" + pszWKT ) + pszWKT = poOutputSRS.ExportToPrettyWkt(0) + print("Target:\n" + pszWKT) return None # -------------------------------------------------------------------- @@ -1256,10 +1274,10 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ # The .cpp version compares on pointers directly, but we cannot # do this with swig object, so just compare the names. if poLayer is not None \ - and poLayer.GetName() == poDstLayer.GetName(): + and poLayer.GetName() == poDstLayer.GetName(): break - if (iLayer == nLayerCount): + if iLayer == nLayerCount: # Shouldn't happen with an ideal driver poDstLayer = None @@ -1269,8 +1287,8 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ # (overwritten). # -------------------------------------------------------------------- if poDstLayer is not None and bOverwrite: - if poDstDS.DeleteLayer( iLayer ) != 0: - print("DeleteLayer() failed when overwrite requested." ) + if poDstDS.DeleteLayer(iLayer) != 0: + print("DeleteLayer() failed when overwrite requested.") return None poDstLayer = None @@ -1307,14 +1325,14 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ elif nCoordDim == 3: eGType = eGType | ogr.wkb25DBit - if poDstDS.TestCapability( ogr.ODsCCreateLayer ) == False: + if not poDstDS.TestCapability(ogr.ODsCCreateLayer): print("Layer " + pszNewLayerName + "not found, and CreateLayer not supported by driver.") return None gdal.ErrorReset() - poDstLayer = poDstDS.CreateLayer( pszNewLayerName, poOutputSRS, \ - eGType, papszLCO ) + poDstLayer = poDstDS.CreateLayer(pszNewLayerName, poOutputSRS, + eGType, papszLCO) if poDstLayer is None: return None @@ -1325,13 +1343,13 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ # Otherwise we will append to it, if append was requested. # -------------------------------------------------------------------- elif not bAppend: - print("FAILED: Layer " + pszNewLayerName + "already exists, and -append not specified.\n" + \ - " Consider using -append, or -overwrite.") + print("FAILED: Layer " + pszNewLayerName + "already exists, and -append not specified.\n" + + " Consider using -append, or -overwrite.") return None else: - if len(papszLCO) > 0: - print("WARNING: Layer creation options ignored since an existing layer is\n" + \ - " being appended to." ) + if papszLCO: + print("WARNING: Layer creation options ignored since an existing layer is\n" + + " being appended to.") # -------------------------------------------------------------------- # Add fields. Default to copy all field. @@ -1342,7 +1360,7 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ # Initialize the index-to-index map to -1's nSrcFieldCount = poSrcFDefn.GetFieldCount() - panMap = [ -1 for i in range(nSrcFieldCount) ] + panMap = [-1] * nSrcFieldCount poDstFDefn = poDstLayer.GetLayerDefn() @@ -1352,20 +1370,20 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ if poDstFDefn is not None: nDstFieldCount = poDstFDefn.GetFieldCount() - for iField in range(len(papszSelFields)): + for papszSelField in papszSelFields: - iSrcField = poSrcFDefn.GetFieldIndex(papszSelFields[iField]) + iSrcField = poSrcFDefn.GetFieldIndex(papszSelField) if iSrcField >= 0: poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iSrcField) - oFieldDefn = ogr.FieldDefn( poSrcFieldDefn.GetNameRef(), - poSrcFieldDefn.GetType() ) - oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() ) - oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() ) + oFieldDefn = ogr.FieldDefn(poSrcFieldDefn.GetNameRef(), + poSrcFieldDefn.GetType()) + oFieldDefn.SetWidth(poSrcFieldDefn.GetWidth()) + oFieldDefn.SetPrecision(poSrcFieldDefn.GetPrecision()) if papszFieldTypesToString is not None and \ - (CSLFindString(papszFieldTypesToString, "All") != -1 or \ - CSLFindString(papszFieldTypesToString, \ - ogr.GetFieldTypeName(poSrcFieldDefn.GetType())) != -1): + (CSLFindString(papszFieldTypesToString, "All") != -1 or + CSLFindString(papszFieldTypesToString, + ogr.GetFieldTypeName(poSrcFieldDefn.GetType())) != -1): oFieldDefn.SetType(ogr.OFTString) @@ -1375,21 +1393,21 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef()) if iDstField >= 0: panMap[iSrcField] = iDstField - elif poDstLayer.CreateField( oFieldDefn ) == 0: + elif poDstLayer.CreateField(oFieldDefn) == 0: # now that we've created a field, GetLayerDefn() won't return NULL if poDstFDefn is None: poDstFDefn = poDstLayer.GetLayerDefn() # Sanity check : if it fails, the driver is buggy if poDstFDefn is not None and \ - poDstFDefn.GetFieldCount() != nDstFieldCount + 1: - print("The output driver has claimed to have added the %s field, but it did not!" % oFieldDefn.GetNameRef() ) + poDstFDefn.GetFieldCount() != nDstFieldCount + 1: + print("The output driver has claimed to have added the %s field, but it did not!" % oFieldDefn.GetNameRef()) else: panMap[iSrcField] = nDstFieldCount nDstFieldCount = nDstFieldCount + 1 else: - print("Field '" + papszSelFields[iField] + "' not found in source layer.") + print("Field '" + papszSelField + "' not found in source layer.") if not bSkipFailures: return None @@ -1405,8 +1423,8 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ for iSrcField in range(nSrcFieldCount): pszFieldName = poSrcFDefn.GetFieldDefn(iSrcField).GetNameRef() bFieldRequested = False - for iField in range(len(papszSelFields)): - if EQUAL(pszFieldName, papszSelFields[iField]): + for papszSelField in papszSelFields: + if EQUAL(pszFieldName, papszSelField): bFieldRequested = True break @@ -1428,33 +1446,33 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ for iField in range(nSrcFieldCount): poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iField) - oFieldDefn = ogr.FieldDefn( poSrcFieldDefn.GetNameRef(), - poSrcFieldDefn.GetType() ) - oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() ) - oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() ) + oFieldDefn = ogr.FieldDefn(poSrcFieldDefn.GetNameRef(), + poSrcFieldDefn.GetType()) + oFieldDefn.SetWidth(poSrcFieldDefn.GetWidth()) + oFieldDefn.SetPrecision(poSrcFieldDefn.GetPrecision()) if papszFieldTypesToString is not None and \ - (CSLFindString(papszFieldTypesToString, "All") != -1 or \ - CSLFindString(papszFieldTypesToString, \ - ogr.GetFieldTypeName(poSrcFieldDefn.GetType())) != -1): + (CSLFindString(papszFieldTypesToString, "All") != -1 or + CSLFindString(papszFieldTypesToString, + ogr.GetFieldTypeName(poSrcFieldDefn.GetType())) != -1): oFieldDefn.SetType(ogr.OFTString) # The field may have been already created at layer creation iDstField = -1 if poDstFDefn is not None: - iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef()) + iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef()) if iDstField >= 0: panMap[iField] = iDstField - elif poDstLayer.CreateField( oFieldDefn ) == 0: + elif poDstLayer.CreateField(oFieldDefn) == 0: # now that we've created a field, GetLayerDefn() won't return NULL if poDstFDefn is None: poDstFDefn = poDstLayer.GetLayerDefn() # Sanity check : if it fails, the driver is buggy if poDstFDefn is not None and \ - poDstFDefn.GetFieldCount() != nDstFieldCount + 1: - print("The output driver has claimed to have added the %s field, but it did not!" % oFieldDefn.GetNameRef() ) + poDstFDefn.GetFieldCount() != nDstFieldCount + 1: + print("The output driver has claimed to have added the %s field, but it did not!" % oFieldDefn.GetNameRef()) else: panMap[iField] = nDstFieldCount nDstFieldCount = nDstFieldCount + 1 @@ -1463,7 +1481,7 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ # For an existing layer, build the map by fetching the index in the destination # layer for each source field if poDstFDefn is None: - print( "poDstFDefn == NULL.\n" ) + print("poDstFDefn == NULL.\n") return None for iField in range(nSrcFieldCount): @@ -1479,29 +1497,30 @@ def SetupTargetLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \ psInfo = TargetLayerInfo() psInfo.poDstLayer = poDstLayer psInfo.poCT = poCT - #psInfo.papszTransformOptions = papszTransformOptions + # psInfo.papszTransformOptions = papszTransformOptions psInfo.panMap = panMap psInfo.iSrcZField = iSrcZField return psInfo -#********************************************************************** +# ********************************************************************** # TranslateLayer() -#********************************************************************** +# ********************************************************************** -def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ - poOutputSRS, bNullifyOutputSRS, \ - eGType, bPromoteToMulti, nCoordDim, eGeomOp, dfGeomOpParam, \ - nCountLayerFeatures, \ - poClipSrc, poClipDst, bExplodeCollections, nSrcFileSize, \ - pnReadFeatureCount, pfnProgress, pProgressArg) : +def TranslateLayer(psInfo, poSrcDS, poSrcLayer, poDstDS, + poOutputSRS, bNullifyOutputSRS, + eGType, bPromoteToMulti, nCoordDim, eGeomOp, dfGeomOpParam, + nCountLayerFeatures, + poClipSrc, poClipDst, bExplodeCollections, nSrcFileSize, + pnReadFeatureCount, pfnProgress, pProgressArg): + # pylint: disable=unused-argument bForceToPolygon = False bForceToMultiPolygon = False bForceToMultiLineString = False poDstLayer = psInfo.poDstLayer - #papszTransformOptions = psInfo.papszTransformOptions + # papszTransformOptions = psInfo.papszTransformOptions poCT = psInfo.poCT panMap = psInfo.panMap iSrcZField = psInfo.iSrcZField @@ -1530,7 +1549,7 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ if nFIDToFetch != ogr.NullFID: - #// Only fetch feature on first pass. + # // Only fetch feature on first pass. if nFeaturesInTransaction == 0: poFeature = poSrcLayer.GetFeature(nFIDToFetch) else: @@ -1552,10 +1571,10 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ eSrcType == ogr.wkbMultiLineString or \ eSrcType == ogr.wkbMultiPolygon or \ eSrcType == ogr.wkbGeometryCollection: - nParts = poSrcGeometry.GetGeometryCount() - nIters = nParts - if nIters == 0: - nIters = 1 + nParts = poSrcGeometry.GetGeometryCount() + nIters = nParts + if nIters == 0: + nIters = 1 for iPart in range(nIters): nFeaturesInTransaction = nFeaturesInTransaction + 1 @@ -1565,19 +1584,19 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ nFeaturesInTransaction = 0 gdal.ErrorReset() - poDstFeature = ogr.Feature( poDstLayer.GetLayerDefn() ) + poDstFeature = ogr.Feature(poDstLayer.GetLayerDefn()) - if poDstFeature.SetFromWithMap( poFeature, 1, panMap ) != 0: + if poDstFeature.SetFromWithMap(poFeature, 1, panMap) != 0: if nGroupTransactions > 0: poDstLayer.CommitTransaction() - print("Unable to translate feature %d from layer %s" % (poFeature.GetFID() , poSrcLayer.GetName() )) + print("Unable to translate feature %d from layer %s" % (poFeature.GetFID(), poSrcLayer.GetName())) return False if bPreserveFID: - poDstFeature.SetFID( poFeature.GetFID() ) + poDstFeature.SetFID(poFeature.GetFID()) poDstGeometry = poDstFeature.GetGeometryRef() if poDstGeometry is not None: @@ -1595,13 +1614,12 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ poDstFeature.SetGeometryDirectly(poDupGeometry) poDstGeometry = poDupGeometry - if nCoordDim == 2 or nCoordDim == 3: - poDstGeometry.SetCoordinateDimension( nCoordDim ) + poDstGeometry.SetCoordinateDimension(nCoordDim) if eGeomOp == GeomOperation.SEGMENTIZE: pass - #if (poDstFeature.GetGeometryRef() is not None and dfGeomOpParam > 0) + # if (poDstFeature.GetGeometryRef() is not None and dfGeomOpParam > 0) # poDstFeature.GetGeometryRef().segmentize(dfGeomOpParam); elif eGeomOp == GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY and dfGeomOpParam > 0: poNewGeom = poDstGeometry.SimplifyPreserveTopology(dfGeomOpParam) @@ -1613,7 +1631,7 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ poClipped = poDstGeometry.Intersection(poClipSrc) if poClipped is None or poClipped.IsEmpty(): # Report progress - nCount = nCount +1 + nCount = nCount + 1 if pfnProgress is not None: pfnProgress(nCount * 1.0 / nCountLayerFeatures, "", pProgressArg) continue @@ -1622,7 +1640,7 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ poDstGeometry = poClipped if poCT is not None: - eErr = poDstGeometry.Transform( poCT ) + eErr = poDstGeometry.Transform(poCT) if eErr != 0: if nGroupTransactions > 0: poDstLayer.CommitTransaction() @@ -1654,14 +1672,14 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ poDstFeature.SetGeometryDirectly(ogr.ForceToMultiLineString(poDstGeometry)) gdal.ErrorReset() - if poDstLayer.CreateFeature( poDstFeature ) != 0 and not bSkipFailures: + if poDstLayer.CreateFeature(poDstFeature) != 0 and not bSkipFailures: if nGroupTransactions > 0: poDstLayer.RollbackTransaction() return False # Report progress - nCount = nCount + 1 + nCount = nCount + 1 if pfnProgress is not None: if nSrcFileSize != 0: if (nCount % 1000) == 0: @@ -1684,9 +1702,10 @@ def TranslateLayer( psInfo, poSrcDS, poSrcLayer, poDstDS, \ return True + if __name__ == '__main__': version_num = int(gdal.VersionInfo('VERSION_NUM')) - if version_num < 1800: # because of ogr.GetFieldTypeName + if version_num < 1800: # because of ogr.GetFieldTypeName print('ERROR: Python bindings of GDAL 1.8.0 or later required') sys.exit(1) diff --git a/postgis.py b/postgis.py index eb93376..d7b5c6f 100644 --- a/postgis.py +++ b/postgis.py @@ -1,13 +1,16 @@ import os import psycopg2 import csv + +#os.environ["GDAL_LIBRARY_PATH"] = "/usr/local/gdal/gdal-3.2.2/lib/libgdal.so" + from lib import ogr2ogr #database connetion strings cwd = os.path.dirname(os.path.realpath(__file__)) secure_path = os.path.join(os.path.dirname(cwd), "secure") -pyscopg2_connection_string = open(os.path.join(secure_path, "psycopg2_connection_string.txt")).read() -gdal_connection_string = open(os.path.join(secure_path, "gdal_connection_string.txt")).read() +pyscopg2_connection_string = open(os.path.join(secure_path, "psycopg2_connection_string_private.txt")).read() +gdal_connection_string = open(os.path.join(secure_path, "gdal_connection_string_private.txt")).read() #sample data paths bird_path = os.path.join(cwd, "data/sample-birdnet.tsv") @@ -64,18 +67,37 @@ def insertBirdNetData(cur, file_path, location_id, timestamp): print(row) -def insertVector(connection_string, in_path,table_name): - ogr2ogr.main(["","-f", "PostgreSQL", "PG:\"%s\"" % (connection_string), in_path, "-a_srs", "EPSG:4326", "-nln", table_name]) +def insertVector(cur, connection_string, in_path,table_name): + #check if table name exists already + cur.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = %s", (table_name,)) + if cur.fetchone() == 1: + raise psycopg2.errors.DuplicateTable("%s table already exists" % table_name) + else: + print("Attempting to create table %s" % table_name) + + #create table + try: + ogr2ogr.main(["","-f", "PostgreSQL", "PG:\"%s\"" % (connection_string), in_path, "-a_srs", "EPSG:4326", "-nln", table_name]) + except (RuntimeError): + raise psycopg2.errors.ConnectionException("GDAL appears to have failed to create table %s" % table_name) + + #add read permissions for geoserver group + try: + cur.execute("GRANT SELECT ON TABLE %s TO geoserver" % (table_name)) + except (psycopg2.errors.UndefinedTable): + pass + if __name__ == "__main__": conn = psycopg2.connect(pyscopg2_connection_string) cur = conn.cursor() - print("There are %i GeoServer tables.\n" % (len(getGSTables(cur)))) - insertVector(gdal_connection_string, extent_path,"gs_extent") + print("There are %i GeoServer tables." % (len(getGSTables(cur)))) + + insertVector(cur, gdal_connection_string, extent_path,"gs_extent") - print("There are %i GeoServer tables.\n" % (len(getGSTables(cur)))) + print("There are %i GeoServer tables." % (len(getGSTables(cur)))) #setupTables(cur) diff --git a/setup/install-gdal.sh b/setup/install-gdal.sh index 1c53b6f..608e387 100644 --- a/setup/install-gdal.sh +++ b/setup/install-gdal.sh @@ -25,14 +25,15 @@ wget http://download.osgeo.org/gdal/3.2.2/gdal-3.2.2.tar.gz tar -xvf gdal-3.2.2.tar.gz cd gdal-3.2.2 #not sure if better to add --with-curl -./configure --prefix=/usr/local/gdal/gdal-3.2.2 --with-python --with-pg +#./configure --prefix=/usr/local/gdal/gdal-3.2.2 --with-python --with-pg +./configure --with-python --with-pg #if necessary first make clean make sudo make install sudo pip3 install gdal-3.2.2/swig/python - +#export PATH="/usr/local/gdal/gdal-3.2.2/bin:$PATH" #confirm postgres support -/usr/local/gdal/gdal-3.2.2/bin/gdalinfo --formats | grep PostGIS -/usr/local/gdal/gdal-3.2.2/bin/ogr2ogr --formats | grep PostgreSQL +gdalinfo --formats | grep PostGIS +ogr2ogr --formats | grep PostgreSQL