Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def epsg_from_crs(crs):
"""
Returns an epsg code from a crs dict or Proj string.
Parameters
----------
crs : dict or string, default None
A crs dict or Proj string
"""
if crs is None:
raise ValueError("No crs provided.")
if isinstance(crs, str):
crs = fiona.crs.from_string(crs)
if not crs:
raise ValueError("Empty or invalid crs provided")
if "init" in crs and crs["init"].lower().startswith("epsg:"):
return int(crs["init"].split(":")[1])
:return: EPSG code (integer)
"""
if self.data is None:
self.read()
if self.data.__class__.__name__ == 'GeoDataFrame':
if self.data.crs is None:
# Make educated guess about projection based on longitude coords
minx = min(self.data.geometry.bounds['minx'])
maxx = max(self.data.geometry.bounds['maxx'])
if minx >= -180.0 and maxx <= 180.0:
self.epsg = 4326
self.data.crs = fiona_crs.from_epsg(self.epsg)
elif minx >= -20026376.39 and maxx <= 20026376.39:
self.epsg = 3857
self.data.crs = fiona_crs.from_epsg(self.epsg)
else:
raise GaiaException('Could not determine data projection.')
return self.epsg
else:
crs = self.data.crs.get('init', None)
if crs:
if ':' in crs:
crs = crs.split(':')[1]
if crs.isdigit():
self.epsg = int(crs)
return self.epsg
# Assume EPSG:4326
self.epsg = 4326
self.data.crs = fiona_crs.from_epsg(self.epsg)
return self.epsg
else:
line_out_file = os.path.join(output_dir, "seismic_lines.shp")
if os.path.exists(line_out_file):
raise ShapeFileExists
# Set up the shapefile schema.
line_schema = {'geometry': 'LineString',
'properties': {'segyfile': 'str',
'line': 'str'
}
}
with fiona.open(line_out_file, "w",
driver="ESRI Shapefile",
crs=crs.from_epsg(26920),
schema=line_schema) as line_out:
for path in utils.walk(input_dir, "\\.se?gy$"):
filebase = os.path.splitext(os.path.basename(path))[0]
# Read in the headers.
segy = obspy.read(path,
headonly=True,
unpack_trace_header=True)
points = []
point_out_file = os.path.join(output_dir, "." +
filebase + '.shp')
def reproject(dataset, epsg):
"""
Reproject a dataset to the specified EPSG code
:param dataset: Dataset to reproject
:param epsg: EPSG code to reproject to
:return: Reprojected data
"""
dataclass = dataset.__class__.__name__
# Run appropriate reprojection method
if dataclass == 'GeoDataFrame':
repro = geopandas.GeoDataFrame.copy(dataclass)
repro[repro.geometry.name] = repro.geometry.to_crs(epsg=epsg)
repro.crs = fiona.crs.from_epsg(epsg)
elif dataclass == 'Dataset':
repro = gdal_reproject(dataset, '', epsg=epsg)
return repro
import collections
import xml.etree.ElementTree as ET
import fiona.crs
import geopandas as gpd
from pandas.io.common import urlopen, urlencode
import pandas as pd
from shapely.geometry import Point, LineString
from six import string_types
OSMData = collections.namedtuple('OSMData', ('nodes', 'waynodes', 'waytags',
'relmembers', 'reltags'))
_crs = fiona.crs.from_epsg(4326)
# Tags to remove so we don't clobber the output. This list comes from
# osmtogeojson's index.js (https://github.com/tyrasd/osmtogeojson)
uninteresting_tags = set([
"source",
"source_ref",
"source:ref",
"history",
"attribution",
"created_by",
"tiger:county",
"tiger:tlid",
"tiger:upload_uuid",
])
def generate_empty_md_graph(name: str,
init_crs: Dict=crs.from_epsg(WGS84)):
"""
Generates an empty multi-directed graph.
Parameters
———————
name : str
The name of the graph
init_crs : dict
The coordinate reference system to be assigned to the graph. Example \
CRS would be `{'init': 'epsg:4326'}`
Returns
——
G : nx.MultiDiGraph
The muti-directed graph
"""
original_projection = featureio.get_epsg()
epsg = original_projection
srs = osr.SpatialReference()
srs.ImportFromEPSG(int(original_projection))
if not srs.GetAttrValue('UNIT').lower().startswith('met'):
epsg = 3857
else:
original_projection = None
feature_df = featureio.read(epsg=epsg)
buffer = GeoSeries(feature_df.buffer(self.buffer_size).unary_union)
buffer_df = GeoDataFrame(geometry=buffer)
buffer_df.crs = feature_df.crs
if original_projection:
buffer_df[buffer_df.geometry.name] = buffer_df.to_crs(
epsg=original_projection)
buffer_df.crs = fiona.crs.from_epsg(original_projection)
return buffer_df