如何通过坐标从JSON中找到几何形状?
我有多个JSON,需要找到几何类型,特别是多边形,点,多边形。下面给出了JSON数据的示例。有什么方法可以使用Python从坐标数据中找到几何形状?我所需要的只是确定其多边形,多边形还是一个点?
{
"geometry":{
"coordinates":[
[
[
128510.83920000028,
6632946.9496
],
[
128500.9031999996,
6632940.271500001
],
[
128464.90429999959,
6632916.0765
],
[
128433.78110000025,
6632895.158500001
],
[
128407.60280000046,
6632877.7302
],
[
128399.27969999984,
6632872.189300001
],
[
128355.1978000002,
6632938.6098
],
[
128333.09640000015,
6632971.901799999
],
[
128309.60800000001,
6633007.1095
],
[
128293.89910000004,
6633030.8453
],
[
128277.66579999961,
6633055.3730999995
],
[
128253.11990000028,
6633101.599300001
],
[
128262.24289999995,
6633115.659700001
],
[
128279.66889999993,
6633144.6799
],
[
128276.79650000017,
6633154.037599999
],
[
128276.2583999997,
6633155.413799999
],
[
128282.10720000044,
6633158.2322
],
[
128349.6233000001,
6633190.7673
],
[
128359.95590000041,
6633178.3037
],
[
128376.82330000028,
6633157.956900001
],
[
128378.63430000003,
6633152.6236000005
],
[
128383.68369999994,
6633142.329500001
],
[
128398.23199999984,
6633119.9964000005
],
[
128431.40840000007,
6633069.0667
],
[
128443.73680000007,
6633048.784
],
[
128498.20990000013,
6632966.115700001
],
[
128510.83920000028,
6632946.9496
]
]
]
}
}
I have multiple JSON and I need to find the geometric type specifically it is Polygon, Point, Multi polygon. Example for the JSON data is given below. Is there any way to find identify the geometry from the coordinates data using python? All I need is to identify if its Polygon, multi polygon or a Point?
{
"geometry":{
"coordinates":[
[
[
128510.83920000028,
6632946.9496
],
[
128500.9031999996,
6632940.271500001
],
[
128464.90429999959,
6632916.0765
],
[
128433.78110000025,
6632895.158500001
],
[
128407.60280000046,
6632877.7302
],
[
128399.27969999984,
6632872.189300001
],
[
128355.1978000002,
6632938.6098
],
[
128333.09640000015,
6632971.901799999
],
[
128309.60800000001,
6633007.1095
],
[
128293.89910000004,
6633030.8453
],
[
128277.66579999961,
6633055.3730999995
],
[
128253.11990000028,
6633101.599300001
],
[
128262.24289999995,
6633115.659700001
],
[
128279.66889999993,
6633144.6799
],
[
128276.79650000017,
6633154.037599999
],
[
128276.2583999997,
6633155.413799999
],
[
128282.10720000044,
6633158.2322
],
[
128349.6233000001,
6633190.7673
],
[
128359.95590000041,
6633178.3037
],
[
128376.82330000028,
6633157.956900001
],
[
128378.63430000003,
6633152.6236000005
],
[
128383.68369999994,
6633142.329500001
],
[
128398.23199999984,
6633119.9964000005
],
[
128431.40840000007,
6633069.0667
],
[
128443.73680000007,
6633048.784
],
[
128498.20990000013,
6632966.115700001
],
[
128510.83920000028,
6632946.9496
]
]
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您拥有的数据没有任何文档吗?从理论上讲,您拥有的可以是通过占据第十七对坐标来形成的多边形……从一堆数字中不可知!
话虽如此...
如果是Geojson数据,则应在
几何>几何> dict中的
类型
条目。如果不这样做,您总是可以从数据的结构中猜测 - 一个点应该具有单个坐标对,多边形应具有外边缘的坐标对列表,以及可能的任何孔的更多列表就像您的示例一样,一个没有孔的多边形),多重法应该具有多边形列表。至少这就是大多数Geodata的外观。
Isn’t there any documentation for the data you have? Theoretically, what you have there could be a polygon formed by taking every seventeenth pair of coordinates... it’s impossible to know from just a bunch of numbers!
Having said that...
If it’s GeoJSON data, there should be a
type
entry inside thegeometry
dict.Failing that, you can always take a guess from the structure of the data — a point should have a single coordinate pair, a polygon should have a list of coordinate pairs for the outer edge, plus possibly more lists for any holes (so it looks like your example is a polygon with no holes), and a multipolygon should have a list of polygons. At least that’s what most geodata would look like.
如果在坐标之前有3个级别的左托架(例如
[[[[[128510.83920000028,6632946.9496] ...
)多边形没有内环或孔。请注意,已发布的JSON文件不是有效的 geojson> geojson 文件几何对象或父特征对象中的属性。这不会使用Shapely或大多数Geojson库来解析。另外,它不是WGS-84,具有纬度(+/- 90)和经度(+/- 180)值,因此如果没有其他信息,就需要做出一些假设。知道它使用的是什么坐标参考系统(CRS)会很有帮助。
但是,如果您具有类似Geojson的JSON数据,则可以通过内容来编程弄清楚该类型。
某些类型并不容易区分,例如从既是点集合的多点''linestring。同样,具有内环和多势的多边形具有相同的结构,但可以检查每个段中的第一个和最后一个点是否相同,这表明多边形。
这是一些测试数据。
这是使用上述数据检查不同几何形状的代码。
输出:
除了打印几何类型外,您还可以将类型分配给几何对象;例如,
geom [“ type”] =“ polygon”
然后将数据导出到新的Geojson文件。If there are 3 levels of left-brackets before the coordinates (e.g.
[[ [128510.83920000028,6632946.9496]...
) as is in your example then it is most likely a Polygon and in this case it's a Polygon with no inner rings or holes.Note the posted JSON file is not a valid GeoJSON file since it does not have required "type" property in the geometry object or on the parent Feature object. This would not parse using Shapely or most GeoJSON libraries. Also, it's not WGS-84 with latitude (+/- 90) and longitude (+/- 180) values so without additional info, you need to make some assumptions. It would be helpful to know what coordinate reference system (CRS) it uses.
However, if you have an arbitrary GeoJSON-like JSON data then you can programmatically figure out the type by the content.
Some types will not be easy to distinguish such as LineString from a MultiPoint which are both a collection of points. Also, a Polygon with inner rings and a MultiLineString have the same structure but could check if the first and last points in each segment are the same, which would indicate a Polygon.
Here is some test data.
Here is code to examine the different geometries with above data.
Output:
In addition to printing the geometry type, you can assign the type to the geometry object; e.g.,
geom["type"] = "Polygon"
then export the data to a new GeoJSON file.