shapefile geojson格式转换

//shp to geojson

首先拿到SimpleFeatureCollection,然后写入geojson

MapconnectionParams = new HashMap();

connectionParams.put("DriverName", "GeoJSON");

connectionParams.put("DatasourceName", file.getAbsolutePath());

OGRDataStoreFactory factory = new JniOGRDataStoreFactory();

OGRDataStore dataStore = (OGRDataStore)factory.createNewDataStore(connectionParams);

dataStore.createSchema(features, true, null);//核心一句

//shp2json

from osgeoimport gdal

from osgeoimport ogr

def shp2gson():

# 为了支持中文路径,请添加下面这句代码

    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO")

# 为了使属性表字段支持中文,请添加下面这句

    gdal.SetConfigOption("SHAPE_ENCODING", "")

strVectorFile ="E:\\example.shp"

    # 注册所有的驱动

    ogr.RegisterAll()

# 打开数据

    ds = ogr.Open(strVectorFile, 0)

if ds ==None:

print("打开文件【%s】失败!", strVectorFile)

return

    print("打开文件【%s】成功!", strVectorFile)

driver = ogr.GetDriverByName("GeoJSON")

if driver ==None:

print("打开驱动失败!")

driver.CopyDataSource(ds, "E:\\fibercable.geojson")

if __name__ =='__main__':

shp2gson()

//geojson to shp

直接从geojson中拿到FeatureCollection,剩下的就是怎么写入shp文件

InputStream in = new FileInputStream(geojson);

FeatureJSON fjson = new FeatureJSON(gjson);

SimpleFeatureCollection fc = (SimpleFeatureCollection)fjson.readFeatureCollection(in);

MapParams = new HashMap();

Params.put("DriverName", "ESRI Shapefile");

Params.put("DatasourceName", shpFile.getAbsolutePath());

OGRDataStoreFactory factory2 = new JniOGRDataStoreFactory();

OGRDataStore dataStore2 = (OGRDataStore) factory2.createNewDataStore(Params);

dataStore2.createSchema(fc, true, null);

dataStore2.dispose();


方法二qgis 太简单了,两句话,qgis封装的好,底层也是ogr

layer=QgsVectorLayer(shpfile,'shp','ogr')

err=QW.writeAsVectorFormat(layer,geojsonfile,'utf-8',None,'GeoJSON')

//parse Feature or FeatureCollectionFeaturefeature=(Feature)GeoJSONFactory.create(json);FeatureCollectionfeatureCollection=(FeatureCollection)GeoJSONFactory.create(json);//parse Geometry from FeatureGeoJSONReaderreader=newGeoJSONReader();Geometrygeometry=reader.read(feature.getGeometry());  geometry=reader.read(featureCollection.getFeatures()[0].getGeometry());//create and serialize a FeatureCollectionListfeatures=newArrayList();Mapproperties=newHashMap();  features.add(newFeature(geometry, properties);GeoJSONWriterwriter=newGeoJSONWriter();GeoJSONjson=writer.write(features);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,511评论 0 17
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,656评论 1 114
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • 电影《鬼吹灯之寻龙诀》(简称《寻龙诀》)根据天下霸唱所著盗墓小说《鬼吹灯》的后四部改编而成,是由乌尔善执导的悬疑动...
    黄青海阅读 828评论 13 14