将KML转换为GeoJson

时间:2013-04-16 14:28:08

标签: php xml xml-parsing kml geojson

我这样做有很多问题,所以我再次来寻求帮助。

我有这个庞大的文件http://d.pr/n/m3q6,其中包含Nielson的所有DMA,我需要用php做什么,不知何故解析每个并获取数据和坐标,但它们必须以这种格式输出< / p>

{"type": "FeatureCollection", "features": [
{
    "type": "Feature",
    "id": "",
    "properties": {
        "dma": "514",
        "name": "Buffalo, NY"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [-79.761951, 42.269861],
                [-79.14959, 42.553193],
                [-79.048361, 42.691959],
                [-78.853455, 42.783961],
                [-78.943961, 42.978357],
                [-78.881611, 43.02363],
                [-78.880756, 43.02955],
                [-78.890068, 43.045268],
                [-78.946342, 43.072051],
                [-79.06977, 43.085841],
                [-79.06467, 43.262754],
                [-78.520257, 43.372277],
                [-77.994839, 43.36526],
                [-77.997291, 43.132981],
                [-77.905934, 43.133562],
                [-77.951044, 43.039544],
                [-77.904562, 43.023492],
                [-77.95633, 42.667882],
                [-78.060469, 42.532888],
                [-77.722965, 42.471217],
                [-77.749931, 41.998782],
                [-77.609815, 41.999367],
                [-77.59813, 41.478577],
                [-78.050728, 41.475103],
                [-78.203422, 41.618157],
                [-78.956057, 41.623863],
                [-78.918856, 41.998119],
                [-79.761374, 41.999068],
                [-79.761951, 42.269861]
            ],

            [
                [-78.933161, 42.963457],
                [-78.934961, 42.965657],
                [-78.934461, 42.964257],
                [-78.933161, 42.963457]
            ],
            [
                [-78.92426, 42.952357],
                [-78.92036, 42.954557],
                [-78.92886, 42.956457],
                [-78.92816, 42.955457],
                [-78.92426, 42.952357]
            ],
            [

                [-78.902609, 42.911768],
                [-78.901122, 42.929442],
                [-78.906739, 42.933805],
                [-78.906259, 42.929557],
                [-78.902609, 42.911768]
            ],
            [

                [-79.064667, 43.078556],
                [-79.071667, 43.078856],
                [-79.073267, 43.080156],
                [-79.071267, 43.083356],
                [-79.064667, 43.078556]

            ],
            [

                [-78.946165, 42.956954],
                [-79.015493, 42.990871],
                [-78.999465, 43.064756],
                [-78.89966, 43.041057],
                [-78.943945, 42.995081],
                [-78.946165, 42.956954]

            ],
            [

                [-78.88566, 43.023557],
                [-78.88836, 43.031957],
                [-78.88826, 43.034457],
                [-78.882868, 43.030771],
                [-78.88566, 43.023557]

            ],
            [

                [-78.956761, 42.958757],
                [-78.961462, 42.959357],
                [-78.963462, 42.961057],
                [-78.958362, 42.961457],
                [-78.956761, 42.958757]

            ]
        ]

    }
  }
]}

任何人都知道如何正确有效地做到这一点我已经被困在这2天没有成功

4 个答案:

答案 0 :(得分:3)

有一种工具可以将(标准)KML转换为geoJSON(参见下面的链接)。但是它用nodejs编写。

https://github.com/tmcw/togeojson#readme

答案 1 :(得分:0)

KMZ到KML

KMZ本质上是一个ZIP存档。只需使用任何zip工具即可解压缩所包含的KML。 Windows用户可能需要将文件扩展名更改为id

从KML到GeoJSON

使用Python实用工具output_data

.zip

什么不起作用

  • ogr2ogr:无法处理KML中的多层

答案 2 :(得分:0)

为响应最近的回答,在每个KML层上进行迭代是一个很好的解决方法,在清理Python和R中的数据时会遇到很多问题,而且我猜想它适用于任何语言。

例如,在R中并使用GDAL,它看起来像:

library(rgdal)
library(sf)
library(plyr)
library(dplyr)
data <- "INPUT KML"
kml_Layers <- ogrListLayers(data)
iterate <- length(kml_Layers) 

iterate_layers <- function(i){
      layer_i <- st_read(data, kml_Layers[i])
      return(layer_i)
    }

loops <- 1:iterate
Result_data <- lapply(iterate_layers, loops)
results <- ldply(Result_data)

writeOGR(results, "Data_geojson", layer="Data_geojson", driver="GeoJSON")

我知道这不是PHP,但是一旦我开始使用KML,它就为我节省了很多时间。

答案 3 :(得分:0)

KMZ 到 GeoJson

            kmz = ZipFile("demo.kmz", 'r')
            kmz.extractall()
            os.rename('doc.kml','demo.kml')
            kml2geojson.main.convert('demo.kml', '')

KML 到 GeoJson

kml2geojson.main.convert('demo.kml', '')