当前位置: 首页 > 知识库问答 >
问题:

在Python中合并两个GEOJSON多边形

淳于恺
2023-03-14

有没有办法在python中合并两个重叠的GEOJSON多边形,返回一个合并的GEOJSON对象

共有2个答案

叶鹭洋
2023-03-14

这里的示例似乎要简洁得多:

from shapely.geometry import Polygon
from shapely.ops import cascaded_union

polygon1 = Polygon([(0, 0), (5, 3), (5, 0)])
polygon2 = Polygon([(0, 0), (3, 10), (3, 0)])

polygons = [polygon1, polygon2]

u = cascaded_union(polygons)
穆飞星
2023-03-14

这就是我如何能够做到这一点使用包/模块json,Geojson,shapely,pyproj,和部分功能工具:

import json
import geojson
from functools import partial
import pyproj
import shapely.geometry
import shapely.ops

# reading into two geojson objects, in a GCS (WGS84)
with open('file1.json') as geojson1:
    poly1_geojson = json.load(geojson1)

with open('file2.json') as geojson2:
    poly2_geojson = json.load(geojson2)


# pulling out the polygons
poly1 = shapely.geometry.asShape(poly1_geojson['features'][2]['geometry'])
poly2 = shapely.geometry.asShape(poly2_geojson['features'][2]['geometry'])

# checking to make sure they registered as polygons
print poly1.geom_type
print poly2.geom_type

# merging the polygons - they are feature collections, containing a point, a polyline, and a polygon - I extract the polygon
# for my purposes, they overlap, so merging produces a single polygon rather than a list of polygons
mergedPolygon = poly1.union(poly2)

# using geojson module to convert from WKT back into GeoJSON format
geojson_out = geojson.Feature(geometry=mergedPolygon, properties={})

# outputting the updated geojson file - for mapping/storage in its GCS format
with open('Merged_Polygon.json', 'w') as outfile:
    json.dump(geojson_out.geometry, outfile, indent=3, encoding="utf-8")
outfile.close()

# reprojecting the merged polygon to determine the correct area
# it is a polygon covering much of the US, and dervied form USGS data, so using Albers Equal Area
project = partial(
    pyproj.transform,
    pyproj.Proj(init='epsg:4326'),
    pyproj.Proj(init='epsg:5070'))

mergedPolygon_proj = shapely.ops.transform(project,mergedPolygon)
 类似资料:
  • 我试图在GeoPandas中找到两个多边形的并集,并输出一个包含两个多边形的点作为其顶点的单个几何体。函数为每个单独的并集提供多边形,但我想要一个多边形。 在上下文中,我使用它将两个行政区域合并为一个区域(即包括一个国家内的一个城镇区)。 下面的例子来自geopandas网站,说明了我想要的: 没有一个输出几何是我所期望的,这是以下内容: 首先,如何使用GeoPandas或shapely从输入多边

  • 我的python3脚本创建了变量,其值是一个shapefiles列表,每个shapefiles都是一个多边形,表示一个地理区域 但它给出的结果是:AttributeError:“Shape”对象没有属性“union” 我看到了另一种方法,它涉及创建一个shapefilewriter对象,然后依次覆盖列表https://gis.stackexchange.com/questions/103033/u

  • 下面是我使用的代码: 有办法让它起作用吗?在中似乎没有简单的方法来获取多边形的边界。

  • 问题内容: 我有两个json 第一个是 第二个是 我想合并它们并有一个像 有没有办法合并它们?如果需要在JSON中更改结构,对我来说也可以 谢谢。 问题答案: 像应该工作。 官方PHP文档中的 array_merge 官方PHP文档中的 json_decode 编辑: 尝试将第二个参数添加到json_decode。这样会将对象转换为关联数组。

  • 问题内容: 我在两个单独的sqlite3数据库中有两个表。数据类型相同,但架构略有不同。我希望它们成为具有相同架构的单个数据库中的单个表 表格1 表2 在两个表之间不是唯一的。我想使用与相同的架构创建另一个表。我希望表1中的条目从0开始,然后从的条目从在的最后一个条目之后开始。 理想情况下,我想刚刚从添加条目,以和“重新索引”的主键,以便它是在同一个升序说:“日期时间”是。 更新 :现在我两个表都

  • 问题内容: 我最近开始使用Python,并且尝试将我的JSON字符串之一与现有JSON字符串连接在一起。我也在与Zookeeper一起工作,所以当我使用Python kazoo库时,我从zookeeper节点获取了现有的json字符串。 如果我打印,它会给我这样的感觉- 但是,如果我这样做,它会像这样打印出来- 这里将有我现有的JSON字符串。现在我有另一个键值对,我需要在出口添加- 以下是我的P