如何舍入 Shapely 多边形中的多边形坐标?
How to round off polygon coordinates in Shapely polygons?
我有一个使用以下代码的复杂用例:
from shapely import wkt
result_polygons = [wkt.loads('POLYGON ((-45.30 5.99, -2.63 5.99, -2.63 1.1, -45.30 1.1, -45.30 5.99))'),
wkt.loads('POLYGON ((-2.63 5.99, -2.63 1.1, -57.63 1.1, -57.63 5.99, -2.63 5.99))'),
wkt.loads('POLYGON ((-4.74 1.1, -37.94 1.1, -45.30 1.10, -45.30 5.99, -12.00 5.99, -4.74 1.1))')]
intersect = wkt.loads('POLYGON ((-4.74 1.1, -57.63 1.1, -57.63 5.99, -12.00 5.99, -4.74 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.30 1.1, -45.30 5.99, -12.00 5.99, -4.74 1.1, -45.30 1.1))')]
for poly in result_polygons: # result_polygons has some polygons
inter = intersect.intersection(poly) # inter has intersection of poly and intersect
same_intersection = False
# avoid adding to list intersections that already exist
for tmp in tmp_intersect:
if tmp.equals(inter):
same_intersection = True
break
if not same_intersection:
tmp_intersect.append(inter)
for i in range(len(tmp_intersect)):
print(tmp_intersect[i].wkt)
这很好用。但问题是我在多边形中没有圆角点。我有以下导致错误结果的数据(由于多边形中浮点值的轻微变化,相同的多边形区域存储在 tmp_intersect 值中)。
result_polygons = [wkt.loads('POLYGON ((-45.3015 5.999999999999997, -2.631 5.999999999999999, -2.631 1.1, -45.3015 1.100000000000006, -45.3015 5.999999999999997))'),
wkt.loads('POLYGON ((-2.631 5.999999999999999, -2.631 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -2.631 5.999999999999999))'),
wkt.loads('POLYGON ((-4.740353646471659 1.1, -37.94482693640743 1.100000000000005, -45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')]
intersect = wkt.loads('POLYGON ((-4.740353646471659 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1, -45.3015 1.100000000000006))')]
有没有办法在我提取多边形点时对其进行四舍五入?
我想,我已经找到解决办法了。可以使用 https://gis.stackexchange.com/a/276512/173433 中提到的以下代码来完成:
shapely.wkt.loads(shapely.wkt.dumps(geom, rounding_precision=n))
其中 rounding_precision=n 是您希望坐标四舍五入到的第 n 个小数点。
所以在下面的代码中使用这段代码对我来说很有效:
from shapely import wkt
result_polygons = [wkt.loads('POLYGON ((-45.3015 5.999999999999997, -2.631 5.999999999999999, -2.631 1.1, -45.3015 1.100000000000006, -45.3015 5.999999999999997))'),
wkt.loads('POLYGON ((-2.631 5.999999999999999, -2.631 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -2.631 5.999999999999999))'),
wkt.loads('POLYGON ((-4.740353646471659 1.1, -37.94482693640743 1.100000000000005, -45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')]
intersect = wkt.loads('POLYGON ((-4.740353646471659 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1, -45.3015 1.100000000000006))')]
round_rp = []
for temp in result_polygons:
round_rp.append(wkt.loads(wkt.dumps(temp, rounding_precision=5)))
round_ti = []
for temp in tmp_intersect:
round_ti.append(wkt.loads(wkt.dumps(temp, rounding_precision=5)))
for poly in round_rp:
inter = shape(intersect).intersection(poly)
same_intersection = False
# avoid adding to list intersections that already exist
for tmp in round_ti:
if tmp.equals(inter):
same_intersection = True
break
if not same_intersection:
round_ti.append(inter)
for i in range(len(round_ti)):
print(round_ti[i].wkt)
我有一个使用以下代码的复杂用例:
from shapely import wkt
result_polygons = [wkt.loads('POLYGON ((-45.30 5.99, -2.63 5.99, -2.63 1.1, -45.30 1.1, -45.30 5.99))'),
wkt.loads('POLYGON ((-2.63 5.99, -2.63 1.1, -57.63 1.1, -57.63 5.99, -2.63 5.99))'),
wkt.loads('POLYGON ((-4.74 1.1, -37.94 1.1, -45.30 1.10, -45.30 5.99, -12.00 5.99, -4.74 1.1))')]
intersect = wkt.loads('POLYGON ((-4.74 1.1, -57.63 1.1, -57.63 5.99, -12.00 5.99, -4.74 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.30 1.1, -45.30 5.99, -12.00 5.99, -4.74 1.1, -45.30 1.1))')]
for poly in result_polygons: # result_polygons has some polygons
inter = intersect.intersection(poly) # inter has intersection of poly and intersect
same_intersection = False
# avoid adding to list intersections that already exist
for tmp in tmp_intersect:
if tmp.equals(inter):
same_intersection = True
break
if not same_intersection:
tmp_intersect.append(inter)
for i in range(len(tmp_intersect)):
print(tmp_intersect[i].wkt)
这很好用。但问题是我在多边形中没有圆角点。我有以下导致错误结果的数据(由于多边形中浮点值的轻微变化,相同的多边形区域存储在 tmp_intersect 值中)。
result_polygons = [wkt.loads('POLYGON ((-45.3015 5.999999999999997, -2.631 5.999999999999999, -2.631 1.1, -45.3015 1.100000000000006, -45.3015 5.999999999999997))'),
wkt.loads('POLYGON ((-2.631 5.999999999999999, -2.631 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -2.631 5.999999999999999))'),
wkt.loads('POLYGON ((-4.740353646471659 1.1, -37.94482693640743 1.100000000000005, -45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')]
intersect = wkt.loads('POLYGON ((-4.740353646471659 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1, -45.3015 1.100000000000006))')]
有没有办法在我提取多边形点时对其进行四舍五入?
我想,我已经找到解决办法了。可以使用 https://gis.stackexchange.com/a/276512/173433 中提到的以下代码来完成:
shapely.wkt.loads(shapely.wkt.dumps(geom, rounding_precision=n))
其中 rounding_precision=n 是您希望坐标四舍五入到的第 n 个小数点。
所以在下面的代码中使用这段代码对我来说很有效:
from shapely import wkt
result_polygons = [wkt.loads('POLYGON ((-45.3015 5.999999999999997, -2.631 5.999999999999999, -2.631 1.1, -45.3015 1.100000000000006, -45.3015 5.999999999999997))'),
wkt.loads('POLYGON ((-2.631 5.999999999999999, -2.631 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -2.631 5.999999999999999))'),
wkt.loads('POLYGON ((-4.740353646471659 1.1, -37.94482693640743 1.100000000000005, -45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')]
intersect = wkt.loads('POLYGON ((-4.740353646471659 1.1, -57.631 1.100000000000007, -57.631 5.999999999999996, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1))')
tmp_intersect = [wkt.loads('POLYGON ((-45.3015 1.100000000000006, -45.3015 5.999999999999997, -12.00490239218409 5.999999999999998, -4.740353646471659 1.1, -45.3015 1.100000000000006))')]
round_rp = []
for temp in result_polygons:
round_rp.append(wkt.loads(wkt.dumps(temp, rounding_precision=5)))
round_ti = []
for temp in tmp_intersect:
round_ti.append(wkt.loads(wkt.dumps(temp, rounding_precision=5)))
for poly in round_rp:
inter = shape(intersect).intersection(poly)
same_intersection = False
# avoid adding to list intersections that already exist
for tmp in round_ti:
if tmp.equals(inter):
same_intersection = True
break
if not same_intersection:
round_ti.append(inter)
for i in range(len(round_ti)):
print(round_ti[i].wkt)