Skip to content

Commit

Permalink
#31 Return valid function in stead of None if layer is None in get_so…
Browse files Browse the repository at this point in the history
…urce_projection
  • Loading branch information
roelderickx committed Sep 10, 2023
1 parent f7d2f33 commit 8a282f8
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions ogr2osm/ogr_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,42 @@ def set_query(self, query):


def __get_source_reprojection_func(self, layer):
if layer is None:
return None

layer_spatial_ref = layer.GetSpatialRef()

spatial_ref = None
if self.source_proj4:
spatial_ref = osr.SpatialReference()
if self.gisorder:
spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
spatial_ref.ImportFromProj4(self.source_proj4)
elif self.source_epsg:
spatial_ref = osr.SpatialReference()
if self.gisorder:
spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
spatial_ref.ImportFromEPSG(self.source_epsg)
elif layer_spatial_ref:
spatial_ref = layer_spatial_ref
self.logger.info("Detected projection metadata:\n%s", str(layer_spatial_ref))
else:
self.logger.info("Layer has no projection metadata, falling back to EPSG:4326")
if not self.gisorder:
spatial_ref = osr.SpatialReference()
spatial_ref.ImportFromEPSG(4326)

# No source proj specified yet? Then default to do no reprojection.
# Some python magic: skip reprojection altogether by using a dummy
# lamdba funcion. Otherwise, the lambda will be a call to the OGR
# reprojection stuff.
reproject = lambda geometry: None

if spatial_ref:
# Destionation projection will *always* be EPSG:4326, WGS84 lat-lon
dest_spatial_ref = osr.SpatialReference()
dest_spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
dest_spatial_ref.ImportFromEPSG(4326)
coord_trans = osr.CoordinateTransformation(spatial_ref, dest_spatial_ref)
reproject = lambda geometry: geometry.Transform(coord_trans)
if layer:
layer_spatial_ref = layer.GetSpatialRef()

spatial_ref = None
if self.source_proj4:
spatial_ref = osr.SpatialReference()
if self.gisorder:
spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
spatial_ref.ImportFromProj4(self.source_proj4)
elif self.source_epsg:
spatial_ref = osr.SpatialReference()
if self.gisorder:
spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
spatial_ref.ImportFromEPSG(self.source_epsg)
elif layer_spatial_ref:
spatial_ref = layer_spatial_ref
self.logger.info("Detected projection metadata:\n%s", str(layer_spatial_ref))
else:
self.logger.info("Layer has no projection metadata, falling back to EPSG:4326")
if not self.gisorder:
spatial_ref = osr.SpatialReference()
spatial_ref.ImportFromEPSG(4326)

if spatial_ref:
# Destionation projection will *always* be EPSG:4326, WGS84 lat-lon
dest_spatial_ref = osr.SpatialReference()
dest_spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
dest_spatial_ref.ImportFromEPSG(4326)
coord_trans = osr.CoordinateTransformation(spatial_ref, dest_spatial_ref)
reproject = lambda geometry: geometry.Transform(coord_trans)

return reproject

Expand Down

0 comments on commit 8a282f8

Please sign in to comment.