Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_geojson_crs(body):
"""Imports a coordinate reference system (CRS) from a GeoJSON tree."""
crs_body = body.get("crs") or body.get("srs")
crs_type = crs_body.get("type", "").upper()
crs_opts = list(crs_body.get("properties").values()) # FIXME: no specific mapping of inputs, each is different
crs = ogr.osr.SpatialReference()
err = -1
if crs_type == "EPSG":
err = crs.ImportFromEPSG(*crs_opts)
elif crs_type == "EPSGA":
err = crs.ImportFromEPSGA(*crs_opts)
elif crs_type == "ERM":
err = crs.ImportFromERM(*crs_opts)
elif crs_type == "ESRI":
err = crs.ImportFromESRI(*crs_opts)
elif crs_type == "USGS":
err = crs.ImportFromUSGS(*crs_opts)
elif crs_type == "PCI":
err = crs.ImportFromPCI(*crs_opts)
# add dirty hack for geojsons used in testbed15-d104
elif crs_type == "NAME" and len(crs_opts) == 1 and ":EPSG:" in crs_opts[0]:
err = crs.ImportFromEPSG(int(crs_opts[0].split(":")[-1]))
def mask(self, ar=None, nodata=0, scale=10):
import ogr
import gdal
from osgeo.gdalconst import GDT_Byte
import numpy as np
import numpy.ma as ma
"""Return an numpy array with a hard mask to exclude areas outside of the place"""
srs_in = ogr.osr.SpatialReference()
srs_in.ImportFromEPSG(self.row['srid'])
aa = self.aa(scale)
image = aa.get_memimage(data_type=GDT_Byte)
ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('/tmp/foo')
lyr = ogr_ds.CreateLayer('place', aa.srs)
geometry = ogr.CreateGeometryFromWkt(self.row['wkt'])
geometry.AssignSpatialReference(srs_in)
geometry.TransformTo(aa.srs)
feature = ogr.Feature(lyr.GetLayerDefn())
feature.SetGeometryDirectly(geometry)
def project(geom, from_epsg=900913, to_epsg=4326):
# source: http://hackmap.blogspot.com/2008/03/ogr-python-projection.html
to_srs = ogr.osr.SpatialReference()
to_srs.ImportFromEPSG(to_epsg)
from_srs = ogr.osr.SpatialReference()
from_srs.ImportFromEPSG(from_epsg)
ogr_geom = ogr.CreateGeometryFromWkb(geom.wkb)
ogr_geom.AssignSpatialReference(from_srs)
ogr_geom.TransformTo(to_srs)
return loads(ogr_geom.ExportToWkb())
def get_srs(self):
import ogr
srs = ogr.osr.SpatialReference()
srs.ImportFromWkt(self.get_srs_wkt())
return srs
def project(geom, from_epsg=900913, to_epsg=4326):
# source: http://hackmap.blogspot.com/2008/03/ogr-python-projection.html
to_srs = ogr.osr.SpatialReference()
to_srs.ImportFromEPSG(to_epsg)
from_srs = ogr.osr.SpatialReference()
from_srs.ImportFromEPSG(from_epsg)
ogr_geom = ogr.CreateGeometryFromWkb(geom.wkb)
ogr_geom.AssignSpatialReference(from_srs)
ogr_geom.TransformTo(to_srs)
return loads(ogr_geom.ExportToWkb())
# Sometimes the EPSG numbers come from the database as strings
try:
srs_spec = int(srs_spec)
except:
pass
srs = ogr.osr.SpatialReference()
if srs_spec is None and default is not None:
return self._get_srs(default, None)
# srs.ImportFromEPSG(default) # Lat/Long in WGS84
elif isinstance(srs_spec, int):
srs.ImportFromEPSG(srs_spec)
elif isinstance(srs_spec, basestring):
srs.ImportFromWkt(srs_spec)
elif isinstance(srs_spec, ogr.osr.SpatialReference):
return srs_spec
else:
raise ValueError("Bad srs somewhere. Source={}, Default = {}"
.format(srs_spec, default))
return srs
def get_transform(self, dest_srs=4326):
"""Get an ogr transform object to convert from the SRS of this
partition to another."""
import ogr
import osr
srs2 = ogr.osr.SpatialReference()
srs2.ImportFromEPSG(dest_srs)
transform = osr.CoordinateTransformation(self.get_srs(), srs2)
return transform
def _get_srs(self, srs_spec=None, default=4326):
srs = ogr.osr.SpatialReference()
if srs_spec is None and default is not None:
return self._get_srs(default, None)
srs.ImportFromEPSG(default) # Lat/Long in WGS84
elif isinstance(srs_spec, int):
srs.ImportFromEPSG(srs_spec)
elif isinstance(srs_spec, basestring):
srs.ImportFromWkt(srs_spec)
elif isinstance(srs_spec, ogr.osr.SpatialReference):
return srs_spec
else:
raise ValueError("Bad srs somewhere. Source={}, Default = {}".format(srs_spec, default))
return srs