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_buffer_init(self):
naive_gser = gpd.GeoSeries([self.geom])
gser = gpd.GeoSeries([self.geom], crs=geom_crs)
# test that we cannot init from a shapely geometry without providing
# its crs
self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape_fp,
self.geom, self.buffer_dists)
# test that we cannot init with a landscape that does not have crs and
# transform information, even when providing the `base_mask` arguments
# properly
for base_mask in [self.geom, naive_gser, gser]:
self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape,
base_mask, self.buffer_dists,
{'base_mask_crs': geom_crs})
self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape,
base_mask, self.buffer_dists, {
'base_mask_crs': geom_crs,
'landscape_crs': landscape_crs
def test_threshold_rules_out_one_but_not_both(self):
# 000
# 00x1
# 00x1
square1 = square_at((0, 0), side_length=3)
square2 = square_at((2, 0), side_length=2)
geometries = geopandas.GeoSeries([square1, square2])
# It's under threshold w.r.t square1 but not square 2
result = resolve_overlaps(geometries, relative_threshold=0.4)
# Expected:
# 000
# 00x1
# 00x1
assert result[0].equals(square1)
assert result[1].equals(square2)
def test_geo_getitem(self):
data = {"A": range(5), "B": range(-5, 0),
"location": [Point(x, y) for x, y in zip(range(5), range(5))]}
df = GeoDataFrame(data, crs=self.crs, geometry='location')
self.assert_(isinstance(df.geometry, GeoSeries))
df['geometry'] = df["A"]
self.assert_(isinstance(df.geometry, GeoSeries))
self.assertEqual(df.geometry[0], data['location'][0])
# good if this changed in the future
self.assert_(not isinstance(df['geometry'], GeoSeries))
self.assert_(isinstance(df['location'], GeoSeries))
data["geometry"] = [Point(x + 1, y - 1) for x, y in zip(range(5), range(5))]
df = GeoDataFrame(data, crs=self.crs)
self.assert_(isinstance(df.geometry, GeoSeries))
self.assert_(isinstance(df['geometry'], GeoSeries))
# good if this changed in the future
self.assert_(not isinstance(df['location'], GeoSeries))
def test_set_geometry_series(self):
# Test when setting geometry with a Series that
# alignment will occur
#
# Reverse the index order
# Set the Series to be Point(i,i) where i is the index
self.df.index = range(len(self.df)-1, -1, -1)
d = {}
for i in range(len(self.df)):
d[i] = Point(i, i)
g = GeoSeries(d)
# At this point, the DataFrame index is [4,3,2,1,0] and the
# GeoSeries index is [0,1,2,3,4]. Make sure set_geometry aligns
# them to match indexes
df = self.df.set_geometry(g)
for i, r in df.iterrows():
self.assertAlmostEqual(i, r['geometry'].x)
self.assertAlmostEqual(i, r['geometry'].y)
assert isinstance(left.index, type(right.index))
if check_dtype:
assert left.dtype == right.dtype, "dtype: %s != %s" % (left.dtype, right.dtype)
if check_series_type:
assert isinstance(left, GeoSeries)
assert isinstance(left, type(right))
if check_crs:
assert left.crs == right.crs
else:
if not isinstance(left, GeoSeries):
left = GeoSeries(left)
if not isinstance(right, GeoSeries):
right = GeoSeries(right, index=left.index)
assert left.index.equals(right.index), "index: %s != %s" % (left.index, right.index)
if check_geom_type:
assert (left.type == right.type).all(), "type: %s != %s" % (
left.type,
right.type,
)
if check_less_precise:
assert geom_almost_equals(left, right)
else:
assert geom_equals(left, right)
if check_index_type:
assert_isinstance(left.index, type(right.index))
if check_dtype:
assert left.dtype == right.dtype, "dtype: %s != %s" % (left.dtype,
right.dtype)
if check_series_type:
assert isinstance(left, GeoSeries)
assert_isinstance(left, type(right))
if check_crs:
assert(left.crs == right.crs)
else:
if not isinstance(left, GeoSeries):
left = GeoSeries(left)
if not isinstance(right, GeoSeries):
right = GeoSeries(right, index=left.index)
assert left.index.equals(right.index), "index: %s != %s" % (left.index,
right.index)
if check_geom_type:
assert (left.type == right.type).all(), "type: %s != %s" % (left.type,
right.type)
if check_less_precise:
assert geom_almost_equals(left, right)
else:
assert geom_equals(left, right)
#Write RGB
cv2.imwrite(os.path.join(save_path, '{}_NeonPlot.png'.format(fname)), plot_rgb)
plot_chm = plot_chm/plot_chm.max() * 255
chm = np.uint8(plot_chm)
draw_detections(chm, image_boxes, image_scores, image_labels, label_to_name=generator.label_to_name, score_threshold=score_threshold, color = (80,127,255))
cv2.imwrite(os.path.join(save_path, '{}_Lidar_NeonPlot.png'.format(plotID)), chm)
#Format name and save
if experiment:
experiment.log_image(os.path.join(save_path, '{}_NeonPlot.png'.format(plotID)),file_name=str(plotID))
experiment.log_image(os.path.join(save_path, '{}_Lidar_NeonPlot.png'.format(plotID)),file_name=str("Lidar_" + plotID))
#calculate recall
s = gp.GeoSeries(map(Point, zip(plot_data.UTM_E, plot_data.UTM_N)))
#Calculate recall
projected_boxes = []
for row in image_boxes:
#Add utm bounds and create a shapely polygon
pbox=create_polygon(row, tile_bounds, cell_size=0.1)
projected_boxes.append(pbox)
#for each point, is it within a prediction?
for index, tree in plot_data.iterrows():
p=Point(tree.UTM_E, tree.UTM_N)
within_polygon=[]
for prediction in projected_boxes:
within_polygon.append(p.within(prediction))
def cloud_to_polygons(pc):
''''
Turn a point cloud with a "Tree" attribute into 2d polygons for calculating IoU
returns a geopandas frame of convex hulls
'''
hulls = [ ]
tree_ids = pc.data.points.user_data.dropna().unique()
for treeid in tree_ids:
points = pc.data.points.loc[pc.data.points.user_data == treeid,["x","y"]].values
s = gp.GeoSeries(map(geometry.Point, zip(points[:,0], points[:,1])))
point_collection = geometry.MultiPoint(list(s))
convex_hull = point_collection.convex_hull
hulls.append(convex_hull)
hulldf = gp.GeoSeries(hulls)
return hulldf
for box in boxes:
#Construct polygon
xmin = box[0]
ymin = box[1]
xmax = box[2]
ymax = box[3]
top_left = [xmin, ymin]
top_right = [xmax, ymin]
bottom_left = [xmin, ymax]
bottom_right = [xmax, ymax]
polygon = geometry.Polygon([top_left, bottom_left, bottom_right, top_right])
polygons.append(polygon)
polygons = gp.GeoSeries(polygons)
return polygons
return var
# GeoSeries and dict inputs need validation to make sure the index matches.
elif isinstance(var, pd.Series):
s = var
elif isinstance(var, dict):
s = gpd.GeoSeries(var)
# List-like inputs do not need index validation, it simply takes on the base data index.
# However, it has to be the exact same length as the base data.
else:
if len(var) != len(df):
raise ValueError(
f"{len(var)} values were passed to {var_name!r}, but {len(df)} were expected."
)
try:
return gpd.GeoSeries(var, index=df.index)
except TypeError:
raise ValueError(
f"{var_name!r} expects a GeoSeries, str, or list-like object as input, but a "
f"{type(var)} was provided instead."
)
if validate:
# df is allowed to have duplicates in its index, but the input series is not the input series
# index must be a superset of the df index
if s.index.duplicated().any():
raise ValueError(
f"The input provided to {var_name!r} contains duplicate values in its index, which "
f"is not allowed. Try using pandas.Series.drop_duplicates or "
f"pandas.DataFrame.drop_duplicates to remove the extra values."
)
if not set(s.index.values).issuperset(set(df.index.values)):