Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
gdf = gpd.read_file(gdf)
if isinstance(im, str):
im = rasterio.open(im)
sindex = gdf.sindex
# use transform_bounds in case the crs is different - no effect if not
if im:
bbox = transform_bounds(im.crs, gdf.crs, *im.bounds)
else:
if isinstance(bbox, Polygon):
bbox = bbox.bounds
if not bbox_crs:
bbox_crs = gdf.crs
bbox = transform_bounds(bbox_crs, gdf.crs, *bbox)
try:
intersectors = list(sindex.intersection(bbox))
except RTreeError:
intersectors = []
return gdf.iloc[intersectors, :]
# use transform_bounds in case the crs is different - no effect if not
if isinstance(bbox, Polygon):
bbox = bbox.bounds
if bbox_crs is None:
try:
bbox_crs = gdf.crs
except AttributeError:
raise ValueError('If `im` and `bbox_crs` are not provided, `gdf`'
'must provide a coordinate reference system.')
else:
bbox_crs = _check_crs(bbox_crs)
bbox_crs = CRS.from_epsg(bbox_crs)
bbox = transform_bounds(bbox_crs, gdf.crs, *bbox)
try:
intersectors = list(sindex.intersection(bbox))
except RTreeError:
intersectors = []
return gdf.iloc[intersectors, :]
from shapely.geometry import Point, Polygon
from shapely.wkt import loads
from collections import Iterable
from ..dbexceptions import GeoError
# Maybe this is only a performance improvement if the data is sorted in
# the generator ...
def gen_index():
for i, wkt, container_obj in containers:
container_geometry = loads(wkt)
yield (i, container_geometry.bounds, (container_obj, container_geometry))
try:
idx = index.Index(gen_index())
except RTreeError:
raise GeoError(
"Failed to create RTree Index. Check that the container generator produced valud output")
# Find the point containment
for contained_coords, contained_obj in containeds:
locations = idx.intersection(contained_coords, objects=True)
if len(contained_coords) == 2:
contained = Point(contained_coords)
elif len(contained_coords) == 4 and not isinstance(contained_coords[0],
Iterable):
# Assume it is bounding box coords. If the elements were iterables ( x,y points ) it
# could be something else.
# bounding boxes are: (minx, miny, maxx, maxy)
(minx, miny, maxx, maxy) = contained_coords