py_tools_ds.geo package

Subpackages

Submodules

py_tools_ds.geo.coord_calc module

py_tools_ds.geo.coord_calc.calc_FullDataset_corner_positions(mask_1bit: numpy.ndarray, assert_four_corners: bool = True, algorithm: str = 'shapely') list[source]

Calculate the image coordinates of the true data corners from a nodata mask.

NOTE: Algorithm ‘shapely’ calculates the corner coordinates of the convex hull of the given mask. Since the convex hull not always reflects all of the true corner coordinates the result can have a limitation in this regard.

Parameters
  • mask_1bit – 2D-numpy array 1bit

  • assert_four_corners – <bool> whether to assert four corners or not

  • algorithm – <str> ‘shapely’ or ‘numpy’ (default: ‘shapely’)

Returns

[UL, UR, LL, LR] as [(ULrow,ULcol),(URrow,URcol),…]

py_tools_ds.geo.coord_calc.corner_coord_to_minmax(corner_coords: Iterable[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float]]) tuple[source]

Return the bounding coordinates for a given set of XY coordinates.

Parameters

corner_coords – four XY tuples of corner coordinates. Their order does not matter.

Returns

xmin,xmax,ymin,ymax

py_tools_ds.geo.coord_calc.get_corner_coordinates(gt, cols, rows)[source]

Return (ULxy, LLxy, LRxy, URxy) in the same coordinate units like the given geotransform.

py_tools_ds.geo.coord_grid module

py_tools_ds.geo.coord_grid.find_nearest_grid_coord(valXY: Sequence[float, float], gt: Sequence[float], rows: int, cols: int, direction: str = 'NW', extrapolate: bool = True) Tuple[float, float][source]
py_tools_ds.geo.coord_grid.get_coord_grid(ULxy: Sequence[float, float], LRxy: Sequence[float, float], out_resXY: Sequence[float, float]) np.ndarray[source]
py_tools_ds.geo.coord_grid.is_coord_grid_equal(gt: Sequence[float], xgrid: Sequence[float], ygrid: Sequence[float], tolerance: float = 0.0) bool[source]

Check if a given GeoTransform exactly matches the given X/Y grid.

Parameters
  • gt – GDAL GeoTransform

  • xgrid – numpy array defining the coordinate grid in x-direction

  • ygrid – numpy array defining the coordinate grid in y-direction

  • tolerance – float value defining a tolerance, e.g. 1e-8

Returns

py_tools_ds.geo.coord_grid.is_point_on_grid(pointXY: Sequence[float, float], xgrid: Sequence[float], ygrid: Sequence[float], tolerance: float = 0.0) bool[source]

Check if a given point is exactly on the given coordinate grid.

Parameters
  • pointXY – (X,Y) coordinates of the point to check

  • xgrid – numpy array defining the coordinate grid in x-direction

  • ygrid – numpy array defining the coordinate grid in y-direction

  • tolerance – float value defining a tolerance, e.g. 1e-8

py_tools_ds.geo.coord_grid.move_shapelyPoly_to_image_grid(shapelyPoly: Union[shapely.geometry.polygon.Polygon, shapely.geometry.geo.box], gt: Sequence[float], rows: int, cols: int, moving_dir: str = 'NW') shapely.geometry.geo.box[source]
py_tools_ds.geo.coord_grid.snap_bounds_to_pixGrid(bounds: Sequence[float], gt: Sequence[float], roundAlg: str = 'auto') Tuple[float, float, float, float][source]

Snap the given bounds to the given grid (defined by gt) under the use of the given round algorithm.

NOTE: asserts equal projections of source and target grid

Parameters
  • bounds – <tuple, list> (xmin, ymin, xmax, ymax)

  • gt – <tuple, list> GDAL geotransform

  • roundAlg – <str> ‘auto’, ‘on’, ‘off’

Returns

py_tools_ds.geo.coord_trafo module

py_tools_ds.geo.coord_trafo.get_utm_zone(longitude: float) int[source]

Get the UTM zone corresponding to the given longitude.

py_tools_ds.geo.coord_trafo.imXY2mapXY(imXY: Union[tuple, numpy.ndarray], gt: Sequence) Union[tuple, numpy.ndarray][source]

Translate the given pixel X/Y locations into geo coordinates according to the given image geotransform.

Parameters
  • imXY – <tuple, np.ndarray> The image coordinates to be translated in the form (x,y) or as np.ndarray [Nx1].

  • gt – <list> GDAL geotransform

Returns

<tuple, np.ndarray> geo coordinate tuple X/Y (mapX, mapY) or np.ndarray [Nx2]

py_tools_ds.geo.coord_trafo.imYX2mapYX(imYX, gt)[source]
py_tools_ds.geo.coord_trafo.latLonToPixel(latLonPairs: List[Sequence[float, float]], geotransform: Sequence, projection: str) List[Sequence[float, float]][source]

Translate the given latitude/longitude pairs into pixel X/Y locations.

Parameters
  • latLonPairs – The decimal lat/lon pairings to be translated in the form [[lat1,lon1],[lat2,lon2]]

  • geotransform – GDAL GeoTransform

  • projection – WKT string

Returns

The pixel translation of the lat/lon pairings in the form [[x1,y1],[x2,y2]]

py_tools_ds.geo.coord_trafo.lonlat_to_pixel(lon, lat, inverse_geo_transform)[source]

Translate the given lon, lat to the grid pixel coordinates in data array (zero start).

py_tools_ds.geo.coord_trafo.mapXY2imXY(mapXY: Union[tuple, numpy.ndarray], gt: Sequence) Union[tuple, numpy.ndarray][source]

Translate the given geo coordinates into pixel locations according to the given image geotransform.

Parameters
  • mapXY – <tuple, np.ndarray> The geo coordinates to be translated in the form (x,y) or as np.ndarray [Nx1].

  • gt – <list> GDAL geotransform

Returns

<tuple, np.ndarray> image coordinate tuple X/Y (column, row) or np.ndarray [Nx2]

py_tools_ds.geo.coord_trafo.mapYX2imYX(mapYX, gt)[source]
py_tools_ds.geo.coord_trafo.pixelToLatLon(pixelPairs: List[Sequence[float, float]], geotransform: Sequence, projection: str) List[Sequence[float, float]][source]

Translate the given pixel X/Y locations into latitude/longitude locations.

Parameters
  • pixelPairs – Image coordinate pairs to be translated in the form [[x1,y1],[x2,y2]]

  • geotransform – GDAL GeoTransform

  • projection – WKT string

Returns

The lat/lon translation of the pixel pairings in the form [[lat1,lon1],[lat2,lon2]]

py_tools_ds.geo.coord_trafo.pixelToMapYX(pixelCoords, geotransform, projection)[source]
py_tools_ds.geo.coord_trafo.reproject_shapelyGeometry(shapelyGeometry, prj_src, prj_tgt)[source]

Reproject any shapely geometry from one projection to another.

Parameters
  • shapelyGeometry – any shapely geometry instance

  • prj_src – GDAL projection as WKT string or EPSG code (‘epsg:1234’ or <EPSG_int>)

  • prj_tgt – GDAL projection as WKT string or EPSG code (‘epsg:1234’ or <EPSG_int>)

py_tools_ds.geo.coord_trafo.transform_GCPlist(gcpList, prj_src, prj_tgt)[source]
Parameters
  • gcpList – <list> list of ground control points in the output coordinate system to be used for warping, e.g. [gdal.GCP(mapX,mapY,mapZ,column,row),…]

  • prj_src – WKT string

  • prj_tgt – WKT string

Returns

py_tools_ds.geo.coord_trafo.transform_any_prj(prj_src: Union[str, int], prj_tgt: Union[str, int], x: Union[float, np.ndarray], y: Union[float, np.ndarray])[source]

Transform X/Y data from any source projection to any target projection.

Parameters
  • prj_src – GDAL projection as WKT string or EPSG code (‘epsg:1234’ or <EPSG_int>)

  • prj_tgt – GDAL projection as WKT string or EPSG code (‘epsg:1234’ or <EPSG_int>)

  • x – X-coordinate(s) to be transformed (either <float> or 1D-<np.ndarray>)

  • y – X-coordinate(s) to be transformed (either <float> or 1D-<np.ndarray>)

Returns

py_tools_ds.geo.coord_trafo.transform_coordArray(prj_src: str, prj_tgt: str, Xarr: numpy.ndarray, Yarr: numpy.ndarray, Zarr: Optional[numpy.ndarray] = None) Sequence[numpy.ndarray][source]

Transform a geolocation array from one projection into another.

HINT: This function is faster than transform_any_prj but works only for geolocation arrays.

Parameters
  • prj_src – WKT string

  • prj_tgt – WKT string

  • Xarr – np.ndarray of shape (rows,cols)

  • Yarr – np.ndarray of shape (rows,cols)

  • Zarr – np.ndarray of shape (rows,cols)

Returns

py_tools_ds.geo.coord_trafo.transform_utm_to_wgs84(easting: float, northing: float, zone: int, south: bool = False)[source]

Transform an UTM coordinate to lon, lat, altitude.

py_tools_ds.geo.coord_trafo.transform_wgs84_to_utm(lon: float, lat: float, zone: int = None)[source]

Return easting, northing, altitude for the given Lon/Lat coordinate.

Parameters
  • lon – longitude

  • lat – latitude

  • zone – UTM zone (if not set it is derived automatically)

py_tools_ds.geo.map_info module

class py_tools_ds.geo.map_info.Geocoding(mapinfo: Optional[Union[list, tuple]] = None, gt: Optional[Union[list, tuple]] = None, prj: str = '')[source]

Bases: object

from_geotransform_projection(gt: Union[list, tuple], prj: str) py_tools_ds.geo.map_info.Geocoding[source]

Create Geocoding object from GDAL GeoTransform + WKT projection string.

HOW COMPUTATION OF RADIANTS WORKS: Building on top of the computation within self.to_geotransform(): gt[1] = math.cos(rotation_rad) * gsd_x gt[2] = math.sin(rotation_rad) * gsd_x

-> we have to solve this equation system to get rotation_rad: gsd_x = gt[2] / math.sin(rotation_rad) gt[1] = math.cos(rotation_rad) * gt[2] / math.sin(rotation_rad) gt[1] * math.sin(rotation_rad) = math.cos(rotation_rad) * gt[2] math.sin(rotation_rad) / math.cos(rotation_rad) = gt[2] / gt[1] math.tan(rotation_rad) = gt[2] / gt[1] rotation_rad = math.atan(gt[2] / gt[1])

Parameters
  • gt – GDAL GeoTransform, e.g. (249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0)

  • prj – GDAL Projection - WKT Format

Returns

instance of Geocoding

from_mapinfo(mapinfo: Union[list, tuple]) py_tools_ds.geo.map_info.Geocoding[source]

Create Geocoding object from ENVI map info.

Parameters

mapinfo – ENVI map info, e.g., [‘UTM’, 1, 1, 192585.0, 5379315.0, 30.0, 30.0, 41, ‘North’, ‘WGS-84’]

Returns

instance of Geocoding

to_geotransform() tuple[source]

Return GDAL GeoTransform list using the attributes of the Geocoding instance.

For equations, see:

https://gis.stackexchange.com/questions/229952/rotate-envi-hyperspectral-imagery-with-gdal/229962

Returns

GDAL GeoTransform, e.g. [249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0]

to_mapinfo()[source]

Return ENVI map info list using the attributes of the Geocoding instance.

Returns

ENVI map info, e.g. [ UTM , 1 , 1 , 256785.0 , 4572015.0 , 30.0 , 30.0 , 43 , North , WGS-84 ]

py_tools_ds.geo.map_info.geotransform2mapinfo(gt: Optional[Union[list, tuple]], prj: Optional[str]) list[source]

Build an ENVI geo info from given GDAL GeoTransform and Projection (compatible with UTM and LonLat projections).

Parameters
  • gt – GDAL GeoTransform, e.g. (249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0)

  • prj – GDAL Projection - WKT Format

Returns

ENVI geo info, e.g. [ UTM , 1 , 1 , 256785.0 , 4572015.0 , 30.0 , 30.0 , 43 , North , WGS-84 ]

Return type

list

py_tools_ds.geo.map_info.get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None)[source]

Return (ULxy, LLxy, LRxy, URxy) in the same coordinate units like the given geotransform.

py_tools_ds.geo.map_info.mapinfo2geotransform(map_info: Optional[list]) tuple[source]

Build GDAL GeoTransform tuple from an ENVI geo info.

Parameters

map_info – ENVI geo info (list), e.g., [‘UTM’, 1, 1, 192585.0, 5379315.0, 30.0, 30.0, 41, ‘North’, ‘WGS-84’]

Returns

GDAL GeoTransform, e.g. [249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0]

py_tools_ds.geo.projection module

py_tools_ds.geo.projection.EPSG2Proj4(EPSG_code: int) str[source]
py_tools_ds.geo.projection.EPSG2WKT(EPSG_code: int) str[source]
py_tools_ds.geo.projection.WKT2EPSG(wkt: str) Optional[int][source]

Transform a WKT string to an EPSG code.

Parameters

wkt – WKT definition

Returns

EPSG code

py_tools_ds.geo.projection.dict_to_proj4(proj4dict: dict) str[source]

Convert a PROJ4-like dictionary into a PROJ4 string.

Parameters

proj4dict – <dict> the PROJ4-like dictionary

py_tools_ds.geo.projection.get_UTMzone(prj: str) Optional[int][source]
py_tools_ds.geo.projection.get_prjLonLat(fmt: str = 'wkt') Union[str, dict][source]

Return standard geographic projection (EPSG 4326) in the WKT or PROJ4 format.

Parameters

fmt – <str> target format - ‘WKT’ or ‘PROJ4’

py_tools_ds.geo.projection.get_proj4info(proj: Optional[Union[str, int]] = None) str[source]

Return PROJ4 formatted projection info for the given projection.

e.g. ‘+proj=utm +zone=43 +datum=WGS84 +units=m +no_defs ‘

Parameters

proj – <str,int> the projection to get PROJ4 formatted info for (WKT or ‘epsg:1234’ or <EPSG_int>)

py_tools_ds.geo.projection.isLocal(prj: Union[str, int, dict]) Optional[bool][source]
Parameters

prj – accepts EPSG, Proj4 and WKT projections

py_tools_ds.geo.projection.isProjectedOrGeographic(prj: Union[str, int, dict]) Optional[str][source]
Parameters

prj – accepts EPSG, Proj4 and WKT projections

py_tools_ds.geo.projection.prj_equal(prj1: Union[None, int, str], prj2: Union[None, int, str]) bool[source]

Check if the given two projections are equal.

Parameters
  • prj1 – projection 1 (WKT or ‘epsg:1234’ or <EPSG_int>)

  • prj2 – projection 2 (WKT or ‘epsg:1234’ or <EPSG_int>)

py_tools_ds.geo.projection.proj4_to_WKT(proj4str: str) str[source]

Convert a PROJ4-like string into a WKT string.

Parameters

proj4str – <dict> the PROJ4-like string

py_tools_ds.geo.projection.proj4_to_dict(proj4: str) dict[source]

Convert a PROJ4-like string into a dictionary.

Parameters

proj4 – <str> the PROJ4-like string

Module contents