Shapely geometries are python classes, so you can simply set a new property in the object.
For my use-cases this has worked, though it may be worth checking for unintended side-effects depending on your scenario.
from shapely import geometry
p = geometry.Point(0, 0)
p.uid = 'unique point'
print(p.uid) # prints: 'unique point'
You have many solutions and I use here a simple example
1) the easiest way
from shapely.geometry import Point, LineString
line = LineString([(1,2),(2,4),(4,5)])
point = Point(2,4)
First, you must determine if the point is within the line (Determine if shapely point is within a linestring/multilinestring)
line.distance(point) < 1e-8
True
print LineString([line.coords[0],point.coords[:][0]])
LINESTRING (1 2, 2 4)
print LineString([point.coords[:][0], line.coords[-1]])
LINESTRING (2 4, 4 5)
2) with the shapely function split
from shapely.ops import split
result = split(line, point)
result.wkt
'GEOMETRYCOLLECTION (LINESTRING (1 2, 2 4), LINESTRING (2 4, 4 5))'
3) from Get the vertices on a LineString either side of a Point
from shapely.geometry import Point,LineString
def split(line_string, point):
coords = line_string.coords
j = None
for i in range(len(coords) - 1):
if LineString(coords[i:i + 2]).intersects(point):
j = i
break
assert j is not None
# Make sure to always include the point in the first group
if Point(coords[j + 1:j + 2]).equals(point):
return coords[:j + 2], coords[j + 1:]
else:
return coords[:j + 1], coords[j:]
line1,line2 = split(line,point)
line1 = LineString(line1)
line2 = LineString(line2)
print line1, line2
LINESTRING (1 2, 2 4) LINESTRING (2 4, 4 5)
4) from Shapely Split LineStrings at Intersections with other LineStrings
# First coords of line (start + end)
coords = [line.coords[0], line.coords[-1]]
# Add the coords from the points
coords += point.coords
# Calculate the distance along the line for each point
dists = [line.project(Point(p)) for p in coords]
# sort the coordinates
coords = [p for (d, p) in sorted(zip(dists, coords))]
lines = [LineString([coords[i], coords[i+1]]) for i in range(len(coords)-1)]
for lin in lines:
print lin
LINESTRING (1 2, 2 4)
LINESTRING (2 4, 4 5)
5) you can also examine and adapt
Best Answer
Once you've calculated the difference between your polygons (let's call this
delta
), take the difference between the exterior ofdelta
and the exterior of your brownpolygon2
:result = delta.exterior.difference(polygon2.exterior)
. Note that the sense of the difference will be significant and it may be tricky to generalize this to all possible polygons. Good luck!