Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
srs = osr.SpatialReference()
srs.ImportFromEPSG(int(original_projection))
if not srs.GetAttrValue('UNIT').lower().startswith('met'):
epsg = 3857
else:
original_projection = None
features_df = features.read(epsg=epsg)
features_gs = features_df.geometry
point_df = self.inputs[1].read(epsg=epsg)[:1]
point_gs = point_df.geometry
features_length = len(features_gs)
min_dist = np.empty(features_length)
for i, feature in enumerate(features_gs):
min_dist[i] = np.min([feature.distance(point_gs[0])])
nearby_df = GeoDataFrame.copy(features_df)
nearby_df['distance'] = min_dist
distance_max = self.distance
nearby_df = nearby_df[(nearby_df['distance'] <= distance_max)]\
.sort_values('distance')
if original_projection:
nearby_df[nearby_df.geometry.name] = \
nearby_df.geometry.to_crs(epsg=original_projection)
return nearby_df
def transform_data(self, outformat=None, epsg=None):
"""
Transform the IO data into the requested format and projection if
necessary.
:param outformat: Output format
:param epsg:
:return:
"""
out_data = geopandas.GeoDataFrame.copy(self.data)
if epsg and str(self.get_epsg()) != epsg:
out_data[out_data.geometry.name] = \
self.data.geometry.to_crs(epsg=epsg)
out_data.crs = fiona.crs.from_epsg(epsg)
if outformat == formats.JSON and self.default_output in (
formats.PANDAS, formats.JSON):
out_json = out_data.to_json()
if out_data.crs:
gj = json.loads(out_json)
gj["crs"] = {
"type": "name",
"properties": {
"name": out_data.crs["init"].upper()
}
}
return json.dumps(gj)
Calculate which features are equal using pandas
:return: result as a GeoDataFrame
"""
first, second = self.inputs[0], self.inputs[1]
first_df = first.read()
second_df = second.read(epsg=first.get_epsg())
first_gs = first_df.geometry
first_length = len(first_gs)
second_gs = second_df.geometry
matches = np.empty(first_length)
for i, first_features in enumerate(first_gs):
matched = [first_features.equals(second_features)
for second_features in second_gs]
matches[i] = True if (True in matched) else False
output_df = GeoDataFrame.copy(first_df)
output_df['equals'] = matches
output_df = output_df[
(output_df['equals'] == 1)].drop('equals', 1)
return output_df
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
def reproject(self, epsg):
repro = geopandas.GeoDataFrame.copy(self.get_data())
repro[repro.geometry.name] = repro.geometry.to_crs(epsg=epsg)
repro.crs = fiona.crs.from_epsg(epsg)
self._data = repro
self._epsg = epsg
# Recompute bounds
geometry = repro['geometry']
geopandas_bounds = geometry.total_bounds
xmin, ymin, xmax, ymax = geopandas_bounds
coords = [[
[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]
]]
metadata = self.get_metadata()
bounds = metadata.get('bounds', {})
bounds['coordinates'] = coords
metadata['bounds'] = bounds
def read(self, epsg=None, **kwargs):
"""
Read vector data from Girder
:param format: Format to return data in (default is GeoDataFrame)
:param epsg: EPSG code to reproject data to
:return: Data in GeoJSON
"""
if self.data is None:
self.save_geojson()
self.data = geopandas.read_file(self.uri)
if self.filters:
self.filter_data()
out_data = self.data
if epsg and self.get_epsg() != epsg:
out_data = geopandas.GeoDataFrame.copy(out_data)
out_data[out_data.geometry.name] = \
self.data.geometry.to_crs(epsg=epsg)
out_data.crs = fiona.crs.from_epsg(epsg)
if format == formats.JSON:
return out_data.to_json()
else:
return out_data
srs.ImportFromEPSG(int(original_projection))
if not srs.GetAttrValue('UNIT').lower().startswith('met'):
epsg = 3857
else:
original_projection = None
first_df = first.read(epsg=epsg)
first_gs = first_df.geometry
first_length = len(first_gs)
second_df = self.inputs[1].read(epsg=epsg)
second_gs = second_df.geometry
min_dist = np.empty(first_length)
for i, first_features in enumerate(first_gs):
min_dist[i] = np.min([first_features.distance(second_features)
for second_features in second_gs])
distance_df = GeoDataFrame.copy(first_df)
distance_df['distance'] = min_dist
distance_df.sort_values('distance', inplace=True)
if original_projection:
distance_df[distance_df.geometry.name] = \
distance_df.geometry.to_crs(epsg=original_projection)
return distance_df