GeoTools——shp转geojson
目录
一、引言
二、代码操作
1、服务端
2、返回数据
三、总结
一、引言
数据库中经常存储的格式是符合OGC标准的WKT或WKB,而在网络中经常传输的格式是json,因此我们会经常把各种数据转为geojson的形式以服务形式发出,供客户端使用。当然你硬要用wkt格式也行,没人管你,自己知道就ok,由于涉及到属性数据只是建议geojson比较方便,毕竟WFS也是用的geojson返回的==
二、代码操作
1、服务端
读取shp数据,解析出simplefeature,使用featurejson转化为json,然后拼接输出。
/*** 后台将shp数据转为geojson,返回* @return*/@RequestMapping("/geojson")@ResponseBodypublic Object shp2geojson(){String shpPath=this.getClass().getResource("/").getFile()+"/file/pointgbk.shp";String jsonPath=this.getClass().getResource("/").getFile()+"file/point.geojson";Map map = new HashMap();//新建json对象FeatureJSON fjson = new FeatureJSON();JSONObject geojsonObject=new JSONObject();geojsonObject.put("type","FeatureCollection");try{//获取featurecollectionFile file = new File(shpPath);ShapefileDataStore shpDataStore = null;shpDataStore = new ShapefileDataStore(file.toURL());//设置编码
/* Charset charset = Charset.forName("GBK");shpDataStore.setCharset(charset);*/String typeName = shpDataStore.getTypeNames()[0];SimpleFeatureSource featureSource = null;featureSource = shpDataStore.getFeatureSource (typeName);SimpleFeatureCollection result = featureSource.getFeatures();SimpleFeatureIterator itertor = result.features();JSONArray array = new JSONArray();//遍历feature转为json对象while (itertor.hasNext()){SimpleFeature feature = itertor.next();StringWriter writer = new StringWriter();fjson.writeFeature(feature, writer);String temp=writer.toString();byte[] b=temp.getBytes("iso8859-1");temp=new String(b,"gbk");System.out.println(temp);JSONObject json = JSON.parseObject(temp);array.add(json);}geojsonObject.put("features",array);itertor.close();long startTime=System.currentTimeMillis();//将json字符串使用字符流写入文件
/* File outputfile=new File(jsonPath);BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(outputfile));bufferedWriter.write(JSON.toJSONString(geojsonObject));bufferedWriter.flush();bufferedWriter.close();*/File outputfile=new File(jsonPath);FileOutputStream fileOutputStream=new FileOutputStream(outputfile);OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");outputStreamWriter.write(JSON.toJSONString(geojsonObject));outputStreamWriter.flush();outputStreamWriter.close();//将json字符串使用字节流写入文件
/* File outputfile=new File(jsonPath);BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outputfile));byte[] bytes= JSON.toJSONString(geojsonObject).getBytes("utf-8");bufferedOutputStream.write(bytes);//fileOutputStream.write(JSON.toJSONString(geojsonObject));bufferedOutputStream.flush();bufferedOutputStream.close();*/long endTime=System.currentTimeMillis();System.out.println("当前程序耗时:"+(endTime-startTime)+"ms");}catch(Exception e){map.put("status", "failure");map.put("message", e.getMessage());e.printStackTrace();}return geojsonObject;}
2、返回数据
{"features":[{"geometry":{"coordinates":[508909.854,299887.23],"type":"Point"},"id":"pointgbk.2","type":"Feature","properties":{"ExtendedEn":"{ 83103 } { L31-20201081-24 L31-20201081-24 }","SubClasses":"AcDbEntity:AcDbText:AcDbText","EntityHand":"1522","Text":"37.24","Linetype":"","Layer":"文字层组-标注"}},{"geometry":{"coordinates":[508884.9711,299895.6874],"type":"Point"},"id":"pointgbk.3","type":"Feature","properties":{"ExtendedEn":"{ 83103 } { L31-20201081-25 L31-20201081-25 }","SubClasses":"AcDbEntity:AcDbText:AcDbText","EntityHand":"1523","Text":"37.23","Linetype":"","Layer":"文字层组-标注"}},……],"type":"FeatureCollection"}
三、总结
- 使用wkt与geojson;
- 代码操作将shp中的feature转化为geojson;
补充:
geojson转shp
Map map = new HashMap();GeometryJSON gjson = new GeometryJSON();try{String strJson = cm.getFileContent(jsonPath);JSONObject json = new JSONObject(strJson);JSONArray features = (JSONArray) json.get("features");JSONObject feature0 = new JSONObject(features.get(0).toString());System.out.println(feature0.toString());String strType = ((JSONObject)feature0.get("geometry")).getString("type").toString();Class<?> geoType = null;switch(strType){case "Point":geoType = Point.class;case "MultiPoint":geoType = MultiPoint.class;case "LineString":geoType = LineString.class;case "MultiLineString":geoType = MultiLineString.class;case "Polygon":geoType = Polygon.class;case "MultiPolygon":geoType = MultiPolygon.class;}//创建shape文件对象File file = new File(shpPath);Map<String, Serializable> params = new HashMap<String, Serializable>();params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);//定义图形信息和属性信息SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();tb.setCRS(DefaultGeographicCRS.WGS84);tb.setName("shapefile");tb.add("the_geom", geoType);tb.add("POIID", Long.class);ds.createSchema(tb.buildFeatureType());//设置编码Charset charset = Charset.forName("GBK");ds.setCharset(charset);//设置WriterFeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);for(int i=0,len=features.length();i<len;i++){String strFeature = features.get(i).toString();Reader reader = new StringReader(strFeature);SimpleFeature feature = writer.next();feature.setAttribute("the_geom",gjson.readMultiPolygon(reader));feature.setAttribute("POIID",i);writer.write();}writer.close();ds.dispose();
GeoTools——shp转geojson相关推荐
- java使用geotools shp转geojson(wtk转geojson) 精度丢失
java 使用geotools将shp转为geojson时,发现geojson的精度丢失,所以导致geojson展示在地图上如下图:(使用wkt转geojson也存在同样问题) 查看api发现是Geo ...
- java 使用gdal_java-gdal实现shp转geojson
安装gdal的话,将shp转geojson会非常简单.本文因项目需要,以java-gdal讲述如何实现.ps:当然只要装有gdal就都可以实现功能. ogr2ogr命令 在控制台执行如下语句: D:\ ...
- shp与geojson互转
前言:最近我们项目有个需求,就是将shp文件转为geojson.网上有很多的网站可以进行shp与geojson互转,但是这种做法并不能集成到我们系统中来,只适合单次调用.于是折腾了好多种办法,终于出来 ...
- Mars3D(含Cesium)数据及服务篇:shp转geojson格式
shp格式介绍 Shapefile文件是ESRI公司ArcGIS平台的常用格式文件,是工业标准的矢量数据文件. Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何 ...
- 用shp制作geoJson格式地图数据(shp convert to geoJson)
本文紧接前文,简单说明利用shp数据制作Echarts支持的geoJson格式的地图数据.本文以北京市通州区各镇的shp数据为例进行说明. 今天是香港回归20周年之际,在这个特殊的日子,祝愿祖国繁荣昌 ...
- Cesium加载建筑物模型(shp转Geojson\3Dtiles)
本文主要介绍cesium加载Geojson和3dtile格式的建筑物模型文件,除此之外还介绍了Cesium工具栏的屏蔽方法.天地图的加载.地球初始状态设置等几个部分的内容,其中又不乏参照.优化诸如: ...
- [Python] GDAL/OGR操作矢量数据(shp、GeoJSON)
GDAL项目旨于地理数据抽象模型对地理数据文件进行读写管理:而其项目下有两大类模块:GDAL和OGR OGR提供操作矢量数据的API,GDAL模块提供栅格数据的API [相关链接] 1.GDAL/OG ...
- Geotools:shape转换为Geojson以及geoJson转wkt
QQ交流群:607330463 GIS开发技术最强交流群 未经允许 禁止转载 可以参考 ```java package com.lcgh.dghy.project.business.yzt.util; ...
- python处理矢量数据格式转换,shp转为geojson,geojson转为pbf,pbf转为geojson
矢量数据转geojson/pbf 需求:读取矢量shp面数据,将其转为可以进行geoserver发布使用的pbf/geojson格式 步骤: 读取shp数据 处理矢量数据,重构为geojson格式 输 ...
最新文章
- ASP.NET 发邮件方法
- 中欧光伏“双反”案:何以平地起波澜?
- VS中怎么新建Web服务器项目,VS中新建网站和新建WEB项目的区别
- Linux命令及文件操作
- 华为隐藏功能扩大内存代码大全_发现将华为手机这3个功能打开,竟然可以将手机性能极限发挥...
- MATLAB之图像与音频信号处理
- ajax返回功能,jquery – 记得ajax在点击返回按钮时添加的数据
- out memory 内存溢出总结
- react替换元素节点_React万字长文面试题梳理
- azkaban 入门简介
- 全网首发:FreeSwitch BANNER支持中文
- git可视化工具Sourcetree使用全攻略(包括各种git冲突解决)
- 使用 ListView 控件展示数据
- 现在90后程序员有必要考证吗?
- json数组转json对象(利用map()函数)
- WIN10桌面图标消失,且右键失效
- python脚本课程_python选课脚本
- 程序员必备的5个工作技能
- 方寸间尽显空间之美,COLMO电热水器的品质沐浴哲学
- dns服务器响应配置,没有配置的dns服务器响应
热门文章
- 代码查看工具_不好用打我 | 六个前端开发在线工具推荐
- lstm原文_对时间序列分类的LSTM全卷积网络的见解
- 蓝牙:协议/服务复用(PSM)
- 经典Java-SpringCloud面试题
- 微信小程序API之audio
- 实验2-2-1 计算分段函数[1] (10 分)
- Angular 自定义分页组件,自定义每页显示个数
- 编写通用计算机代码,计算机源代码编写规范(VB版本).doc
- 怎么设置php 中小窗口浮动,CSS_闭合浮动元素超级简单的方法,无意中看到的一个非常不错的 - phpStudy...
- laravel validate 设置为中文(验证提示为中文)