验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

Python shapefile转GeoJson的方法有哪些

阅读:560 来源:乙速云 作者:代码code

Python shapefile转GeoJson的方法有哪些

      GeoJson的简要介绍

      GeoJson是用json的语法表达和存储地理数据,可以说是json的子集。

      GeoJson以键值对的形式保存原有对象的信息,具有轻量化、易解析等优点。

      GeoJson包括的地理要素有Point(点)、 MultiPoint(多点)、 LineString(线)、MultiLineString(多线)、 Polygon(面)、 MultiPolygon(多面)、 GeometryCollection(几何集合)

      这些地理要素包括在geometry的type属性中,并且不同的type具有不同的coordinates值。

           {
               "type": "MultiPoint",
               "coordinates": [
                   [100.0, 0.0],
                   [101.0, 1.0]
               ]
           }
       
       
           {
               "type": "MultiPolygon",
               "coordinates": [
                   [
                       [
                           [102.0, 2.0],
                           [103.0, 2.0],
                           [103.0, 3.0],
                           [102.0, 3.0],
                           [102.0, 2.0]
                       ]
                   ],
                   [
                       [
                           [100.0, 0.0],
                           [101.0, 0.0],
                           [101.0, 1.0],
                           [100.0, 1.0],
                           [100.0, 0.0]
                       ],
                       [
                           [100.2, 0.2],
                           [100.2, 0.8],
                           [100.8, 0.8],
                           [100.8, 0.2],
                           [100.2, 0.2]
                       ]
                   ]
               ]
           }

      两种将shapefile文件转换为GeoJson的方式

      1. 使用geopandas

      核心代码:geopandas.GeoSeries 和out_data.to_file

      import geopandas as gpd
       
      def shp2geojson_gpd(shp_file, geojson_file):
          """
          将shapefile格式的文件转化为geojson
          :param shp_file: 需要转换的shapefile文件名,投影信息可以缺失,也可以指定
          :param geojson_file: 转换输出的geojson文件名
          """
       
          if os.path.exists(geojson_file):
              os.remove(geojson_file)
       
          out_data = gpd.read_file(shp_file)
          crs = out_data.crs
          out_data = gpd.GeoSeries(out_data.geometry, crs=crs)
          out_data.to_file(geojson_file, driver='GeoJSON', encoding="utf-8")
          print("successfully convert shapefile to geojson")

      使用geopandas转换的时候两行核心代码即可搞定,简单粗暴。但是在实践过程中发现,采用geopandas转换后的GeoJson文件并没有保留shapefile中的属性properities信息,如area, name等,如下图所示:

      Python shapefile转GeoJson的方法有哪些

      2. 使用gdal

      import gdal 
      import ogr
      import os
       
      def shp2geojson_gdal(shp_file, geojson_file):
          gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
          gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
          src_ds = ogr.Open(shp_file)
          src_layer = src_ds.GetLayer(0)
       
          # 创建结果Geojson
          baseName = os.path.basename(geojson_file)
          dst_driver = ogr.GetDriverByName('GeoJSON')
          dst_ds = dst_driver.CreateDataSource(geojson_file)
          if dst_ds.GetLayer(baseName):
              dst_ds.DeleteLayer(baseName)
          dst_layer = dst_ds.CreateLayer(baseName, src_layer.GetSpatialRef())
          dst_layer.CreateFields(src_layer.schema)
          dst_feat = ogr.Feature(dst_layer.GetLayerDefn())
       
          # 生成结果文件
          for feature in src_layer:
              dst_feat.SetGeometry(feature.geometry())
              for j in range(feature.GetFieldCount()):
                  dst_feat.SetField(j, feature.GetField(j))
              dst_layer.CreateFeature(dst_feat)
       
          del dst_ds
          del src_ds
          print("successfully convert shapefile to geojson")

      结果包含原始shapefile文件中的属性信息:

      Python shapefile转GeoJson的方法有哪些

    分享到:
    *特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
    相关文章
    {{ v.title }}
    {{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
    你可能感兴趣
    推荐阅读 更多>