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_feature(self):
import shapely.geometry
shape = shapely.geometry.LineString([(0, 0), (1, 1), (1, 0)])
props = {
'boundary': 'administrative',
'admin_level': '2',
}
meta = make_test_metadata()
out_min_zoom = self.boundaries.fn(shape, props, None, meta)
self.assertEquals(8, out_min_zoom)
def test_parallel_offset_linestring(self):
line1 = LineString([(0, 0), (10, 0)])
left = line1.parallel_offset(5, 'left')
self.assertEqual(left, LineString([(0, 5), (10, 5)]))
right = line1.parallel_offset(5, 'right')
self.assertEqual(right, LineString([(10, -5), (0, -5)]))
right = line1.parallel_offset(-5, 'left')
self.assertEqual(right, LineString([(10, -5), (0, -5)]))
left = line1.parallel_offset(-5, 'right')
self.assertEqual(left, LineString([(0, 5), (10, 5)]))
# by default, parallel_offset is right-handed
self.assertEqual(line1.parallel_offset(5), right)
line2 = LineString([(0, 0), (5, 0), (5, -5)])
self.assertEqual(line2.parallel_offset(2, 'left', resolution=1),
LineString([(0, 2), (5, 2), (7, 0), (7, -5)]))
self.assertEqual(line2.parallel_offset(2, 'left', join_style=2,
resolution=1),
LineString([(0, 2), (7, 2), (7, -5)]))
Returns
-------
an interpolated shapely.geometry.Polygon class instance.
"""
# remove last (duplex) point to build a LineString from the LinearRing
line = shpg.LineString(np.asarray(polygon.exterior.xy).T)
e_line = []
for distance in np.arange(0.0, line.length, dx):
e_line.append(*line.interpolate(distance).coords)
e_line = shpg.LinearRing(e_line)
i_lines = []
for ipoly in polygon.interiors:
line = shpg.LineString(np.asarray(ipoly.xy).T)
if line.length < 3*dx:
continue
i_points = []
for distance in np.arange(0.0, line.length, dx):
i_points.append(*line.interpolate(distance).coords)
i_lines.append(shpg.LinearRing(i_points))
return shpg.Polygon(e_line, i_lines)
img_polygon = Polygon([(0, 0), (w - 1, 0), (w - 1, h - 1), (0, h - 1)])
canvas = np.zeros((h, w, 3), dtype=np.uint8)
mask = np.zeros((h, w, 3), dtype=np.uint8)
# Draw the boxes
for i in range(5):
# And make a new image with the projected field
linea = LineString([(field_points2d[i][0, :]),
(field_points2d[i][1, :])])
lineb = LineString([(field_points2d[i][1, :]),
(field_points2d[i][2, :])])
linec = LineString([(field_points2d[i][2, :]),
(field_points2d[i][3, :])])
lined = LineString([(field_points2d[i][3, :]),
(field_points2d[i][0, :])])
if i == 0:
polygon0 = Polygon([(field_points2d[i][0, :]),
(field_points2d[i][1, :]),
(field_points2d[i][2, :]),
(field_points2d[i][3, :])])
intersect0 = img_polygon.intersection(polygon0)
if not intersect0.is_empty:
pts = np.array(list(intersect0.exterior.coords), dtype=np.int32)
pts = pts[:, :].reshape((-1, 1, 2))
cv2.fillConvexPoly(mask, pts, (255, 255, 255))
if pd == distance:
return [
LineString(coords[:i+1]),
LineString(coords[i:])]
if pd > distance:
#print("This case")
cp = line.interpolate(distance)
#print("cp: %s"%(cp,))
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]
if i == len(coords)-1:
cp = line.interpolate(distance)
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]
ext = ".shp"
if fmt == "GeoJSON":
ext = ".geojson"
filepath = os.path.join(folderpath, "{0}{1}".format(layername, ext))
self.__dest_folder(folderpath, crtfld)
if fmt == "GeoJSON" and os.path.isfile(filepath):
os.remove(filepath)
out_crs = from_epsg(epsg_cd)
with collection(filepath, "w", fmt, schema, crs=out_crs) as output:
line = LineString(coords)
geom = mapping(line)
if self.__antimeridian:
line_t = self.__antiMeridianCut(geom)
else:
line_t = geom
output.write({
'properties': {
'prop': prop
},
'geometry': line_t
})
self.__logger.info("{0} succesfully created!".format(fmt))
def check_overlap(edge1, edge2):
# imrpoved version of the comparison where
# edges may overlap but not along entire length
ls_edge1 = LineString(edge1).buffer(0.0001)
ls_edge2 = LineString(edge2)
isection = ls_edge1.intersection(ls_edge2)
if isection.geom_type == "LineString":
if isection.length > 0.001:
#print isection
coords = isection.coords[:]
return True, coords
else:
return False, None
else:
return False, None
#
# Shapely polygon conversion requires at least 3 coordinates
if len(self.exterior) == 0:
return []
if len(self.exterior) in [1, 2]:
ls = self.to_line_string(closed=False)
ls_clipped = ls.clip_out_of_image(image)
assert len(ls_clipped) <= 1
if len(ls_clipped) == 0:
return []
return [self.deepcopy(exterior=ls_clipped[0].coords)]
h, w = image.shape[0:2] if ia.is_np_array(image) else image[0:2]
poly_shapely = self.to_shapely_polygon()
poly_image = shapely.geometry.Polygon([(0, 0), (w, 0), (w, h), (0, h)])
multipoly_inter_shapely = poly_shapely.intersection(poly_image)
ignore_types = (shapely.geometry.LineString,
shapely.geometry.MultiLineString,
shapely.geometry.point.Point,
shapely.geometry.MultiPoint)
if isinstance(multipoly_inter_shapely, shapely.geometry.Polygon):
multipoly_inter_shapely = shapely.geometry.MultiPolygon(
[multipoly_inter_shapely])
elif isinstance(multipoly_inter_shapely,
shapely.geometry.MultiPolygon):
# we got a multipolygon from shapely, no need to change anything
# anymore
pass
elif isinstance(multipoly_inter_shapely, ignore_types):
# polygons that become (one or more) lines/points after clipping
# are here ignored
multipoly_inter_shapely = shapely.geometry.MultiPolygon([])
elif isinstance(multipoly_inter_shapely,
def calculate_path_loss(self, receiver,
frequency, environment, seed_value, iterations):
temp_line = LineString([
(receiver.coordinates[0],
receiver.coordinates[1]),
(self.transmitter.coordinates[0],
self.transmitter.coordinates[1])]
)
strt_distance = temp_line.length
ant_height = self.transmitter.ant_height
ant_type = 'macro'
los_distance = 250
if strt_distance < los_distance :
type_of_sight = 'los'
else:
def enumerate_sub_bcs(self):
# dredge_grid already has some of the machinery
grid=self.model.grid
edges=dfm_grid.polyline_to_boundary_edges(grid,np.array(self.geom.coords))
self.model.log.info("MultiBC will be applied over %d edges"%len(edges))
self.sub_bcs=[]
for j in edges:
seg=grid.nodes['x'][ grid.edges['nodes'][j] ]
sub_geom=geometry.LineString(seg)
# This slightly breaks the abstraction -- in theory, the caller
# can edit all of self's values up until write() is called, yet
# here we are grabbing the values at time of instantiation of self.
# hopefully it doesn't matter, especially since geom and model
# are handled explicitly.
sub_kw=dict(self.saved_kw) # copy original
sub_kw['geom']=sub_geom
sub_kw['name']="%s%04d"%(self.name,j)
# this is only guaranteed to be a representative element
sub_kw['grid_edge']=j
# this, if set, is all the elements
sub_kw['grid_edges']=[j]
j_cells=grid.edges['cells'][j]
assert j_cells.min()<0
assert j_cells.max()>=0
c=j_cells.max()