Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_towgs84():
"""+towgs84 is preserved"""
proj4 = ('+proj=lcc +lat_1=49 +lat_2=46 +lat_0=47.5 '
'+lon_0=13.33333333333333 +x_0=400000 +y_0=400000 +ellps=bessel '
'+towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232 '
'+units=m +wktext +no_defs')
assert 'towgs84' in crs.from_string(proj4)
def test_wktext():
"""Test +wktext parameter is preserved."""
proj4 = ('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 '
'+x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext '
'+no_defs')
assert 'wktext' in crs.from_string(proj4)
print(len(list(c)))
except:
print(c.closed)
raise
###############################################################################
c = fiona.open('/gdata/GSHHS_c.shp')
c.driver
###############################################################################
c.crs
###############################################################################
from fiona.crs import to_string
to_string(c.crs)
###############################################################################
from fiona.crs import from_string
from_string(
"+datum=WGS84 +ellps=WGS84 +no_defs +proj=longlat")
###############################################################################
from fiona.crs import from_epsg
from_epsg(3857)
###############################################################################
len(c)
###############################################################################
c.bounds
def pick_projection(project):
use_proj, out_crs = None, None
if project.lower() in ('local', 'utm'):
use_proj = project.lower()
elif os.path.exists(project):
# Is a file
with open(project) as f:
out_crs = fiona.crs.from_string(f.read())
elif project[:5].lower() == 'epsg:':
# Is an epsg code
_, epsg = project.split(':')
out_crs = fiona.crs.from_epsg(int(epsg))
else:
# Assume it's a proj4 string.
# fiona.crs.from_string returns {} if it isn't.
out_crs = fiona.crs.from_string(project)
return use_proj, out_crs
if epsg == 4326:
value = WGS84().value
# Add a special check for init keys in value dictionary.
if value is not None:
if 'init' in value and list(value.values())[0].startswith('epsg'):
epsg = int(list(value.values())[0].split(':')[1])
value = None
if value is None:
if proj4 is not None:
value = from_string(proj4)
elif epsg is not None:
sr = SpatialReference()
sr.ImportFromEPSG(epsg)
value = from_string(sr.ExportToProj4())
else:
msg = 'A value dictionary, PROJ.4 string, or EPSG code is required.'
raise ValueError(msg)
else:
# Remove unicode to avoid strange issues with proj and fiona.
for k, v in value.items():
if isinstance(v, six.string_types):
value[k] = str(v)
else:
try:
value[k] = v.tolist()
# this may be a numpy arr that needs conversion
except AttributeError:
continue
sr = SpatialReference()
project = project or 'default'
if isinstance(project, dict):
return project
elif isinstance(project, string_types):
if project.lower() == 'file':
out_crs = file_crs if file_crs is not None else 'file'
# Is an epsg code
elif project.lower()[:5] == 'epsg:':
out_crs = fiona.crs.from_epsg(int(project.split(':')[1]))
elif project.lower() in METHODS:
try:
out_crs = fiona.crs.from_string(generateproj4(project, bounds, file_crs))
except ValueError:
return project
# Is a file
elif os.path.exists(project):
with open(project) as f:
out_crs = fiona.crs.from_string(f.read())
else:
# Assume it's a proj4 string.
# fiona.crs.from_string returns {} if it isn't.
out_crs = fiona.crs.from_string(project)
return out_crs
elif project.lower() in METHODS:
try:
out_crs = fiona.crs.from_string(generateproj4(project, bounds, file_crs))
except ValueError:
return project
# Is a file
elif os.path.exists(project):
with open(project) as f:
out_crs = fiona.crs.from_string(f.read())
else:
# Assume it's a proj4 string.
# fiona.crs.from_string returns {} if it isn't.
out_crs = fiona.crs.from_string(project)
return out_crs
def to_geopandas(raster, **kwargs):
"""
Convert GeoRaster to GeoPandas DataFrame, which can be easily exported to other types of files
and used to do other types of operations.
The DataFrame has the geometry (Polygon), row, col, value, x, and y values for each cell
Usage:
df = gr.to_geopandas(raster)
"""
df = to_pandas(raster, **kwargs)
df['geometry'] = df.apply(squares, georaster=raster, axis=1)
df = gp.GeoDataFrame(df, crs=from_string(raster.projection.ExportToProj4()))
return df
raise ValueError(msg)
else:
# Remove unicode to avoid strange issues with proj and fiona.
for k, v in value.items():
if isinstance(v, six.string_types):
value[k] = str(v)
else:
try:
value[k] = v.tolist()
# this may be a numpy arr that needs conversion
except AttributeError:
continue
sr = SpatialReference()
sr.ImportFromProj4(to_string(value))
self.value = from_string(sr.ExportToProj4())
try:
assert self.value != {}
except AssertionError:
msg = 'Empty CRS: The conversion to PROJ.4 may have failed. The CRS value is: {0}'.format(value)
raise ValueError(msg)