def check_axis_mapping(spatial_ref: osr.SpatialReference): """Make sure our Axis Mapping strategy is correct (according to our arbitrary convention) We set this in opposition to what GDAL3 Thinks is the default because that's what most of our old Geodatabases and Shapefiles seem to use. Raises: VectorBaseException: [description] """ if spatial_ref is None: raise VectorBaseException('No spatial_ref found to verify') if spatial_ref.GetAxisMappingStrategy( ) != osr.OAMS_TRADITIONAL_GIS_ORDER: _p4, axis_strat = VectorBase.get_srs_debug(spatial_ref) VectorBase.log.warning( 'Axis mapping strategy is: "{}". This may cause axis flipping problems' .format(axis_strat))
def get_transform_from_raster( in_srs: osr.SpatialReference, raster_path: str ) -> (osr.SpatialReference, osr.CoordinateTransformation): """Get a transform between a given SRS and the SRS from a raster Args: in_srs ([type]): input SRS osr ([type]): [description] Raises: VectorBaseException: [description] Returns: [type]: [description] """ if in_srs is None: raise VectorBaseException( 'No input spatial ref found. Has this layer been created or loaded?' ) out_ds = gdal.Open(raster_path) out_spatial_ref = osr.SpatialReference() out_spatial_ref.ImportFromWkt(out_ds.GetProjectionRef()) in_proj4, in_ax_strategy = VectorBase.get_srs_debug(in_srs) out_proj4, out_ax_strategy = VectorBase.get_srs_debug(out_spatial_ref) # https://github.com/OSGeo/gdal/issues/1546 out_spatial_ref.SetAxisMappingStrategy(in_srs.GetAxisMappingStrategy()) VectorBase.log.debug( 'Input spatial reference is "{}" Axis Strategy: "{}"'.format( in_proj4, in_ax_strategy)) VectorBase.log.debug( 'Output spatial reference is "{}" Axis Strategy: "{}"'.format( out_proj4, out_ax_strategy)) # This check will throw a warning into the log if we've got anything but TRADITIONAL axis strategy VectorBase.check_axis_mapping(in_srs) transform = VectorBase.get_transform(in_srs, out_spatial_ref) return out_spatial_ref, transform
def get_srs_debug(spatial_ref: osr.SpatialReference) -> [str, str]: """useful method for printing spatial ref information to the log Args: spatial_ref (osr.SpatialReference): [description] Returns: [str, str]: [description] """ order = spatial_ref.GetAxisMappingStrategy() order_str = str(order) if order == 0: order_str = 'OAMS_TRADITIONAL_GIS_ORDER' elif order == 1: order_str = 'OAMS_AUTHORITY_COMPLIANT' elif order == 2: order_str = 'OAMS_CUSTOM' return [spatial_ref.ExportToProj4(), order_str]
def get_transform_from_epsg( in_srs: osr.SpatialReference, epsg: int) -> (osr.SpatialReference, osr.CoordinateTransformation): """Transform a spatial ref using an epsg code provided This is done explicitly and includes a GetAxisMappingStrategy check to account for GDAL3's projection differences. Args: in_srs ([type]): input SRS epsg ([type]): [description] Returns: [type]: [description] """ if in_srs is None: raise VectorBaseException( 'No input spatial ref found. Has this layer been created or loaded?' ) out_spatial_ref = VectorBase.get_srs_from_epsg(epsg) # https://github.com/OSGeo/gdal/issues/1546 # Set the axis mapping to be the same as the input. This might prove problematic in cases where the input is out_spatial_ref.SetAxisMappingStrategy(in_srs.GetAxisMappingStrategy()) in_proj4, in_ax_strategy = VectorBase.get_srs_debug(in_srs) out_proj4, out_ax_strategy = VectorBase.get_srs_debug(out_spatial_ref) VectorBase.log.debug( 'Input spatial reference is "{}" Axis Strategy: "{}"'.format( in_proj4, in_ax_strategy)) VectorBase.log.debug( 'Output spatial reference is "{}" Axis Strategy: "{}"'.format( out_proj4, out_ax_strategy)) transform = VectorBase.get_transform(in_srs, out_spatial_ref) return out_spatial_ref, transform