geotools实现shp数据的缓冲区分析
2017-06-30 07:28
417 查看
概述:
本文讲述如何在geotools中实现shp数据的缓冲区分析并保存到shp文件中。
效果:
实现代码:
package com.lzugis.geotools;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygon;
public class ShapeBuffer {
/**
* 缓冲区分析
* @param geom
* @param distance
* @return
*/
public Geometry calBuffer(Geometry geom, double distance){
return geom.buffer(distance);
}
public static void main(String[] args){
long start = System.currentTimeMillis();
ShapeBuffer geoR = new ShapeBuffer();
String shpfile = "/Users/lzugis/Documents/chinadata/capital.shp";
String buffile = "/Users/lzugis/Documents/chinadata/capital_buffer.shp";
try{
//读取shp文件
File file = new File(shpfile);
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();
//创建shape文件对象
File fileBuf = new File(buffile);
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put( ShapefileDataStoreFactory.URLP.key, fileBuf.toURI().toURL() );
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
SimpleFeatureType sft = featureSource.getSchema();
List<AttributeDescriptor> attrs = sft.getAttributeDescriptors();
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
for(int i=0;i<attrs.size();i++){
AttributeDescriptor attr = attrs.get(i);
String fieldName = attr.getName().toString();
if(fieldName=="the_geom"){
tb.add(fieldName, Polygon.class);
}
else{
tb.add(fieldName, String.class);
}
}
ds.createSchema(tb.buildFeatureType());
//设置编码
ds.setCharset(charset);
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
while (itertor.hasNext())
{
SimpleFeature feature = itertor.next();
SimpleFeature featureBuf = writer.next();
featureBuf.setAttributes(feature.getAttributes());
Geometry geo = (Geometry)feature.getAttribute("the_geom");
Geometry geoBuffer = geoR.calBuffer(geo, 1.5);
featureBuf.setAttribute("the_geom", geoBuffer);
}
writer.write();
writer.close();
itertor.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("共耗时"+(System.currentTimeMillis() - start)+"ms");
}
}
---------------------------------------------------------------------------------------------------------------技术博客CSDN:http://blog.csdn.NET/gisshixisheng博客园:http://www.cnblogs.com/lzugis/
在线教程http://edu.csdn.Net/course/detail/799Githubhttps://github.com/lzugis/联系方式q q:1004740957e-mail:niujp08@qq.com公众号:lzugis15Q Q 群:452117357(webgis) 337469080(Android)
本文讲述如何在geotools中实现shp数据的缓冲区分析并保存到shp文件中。
效果:
实现代码:
package com.lzugis.geotools;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygon;
public class ShapeBuffer {
/**
* 缓冲区分析
* @param geom
* @param distance
* @return
*/
public Geometry calBuffer(Geometry geom, double distance){
return geom.buffer(distance);
}
public static void main(String[] args){
long start = System.currentTimeMillis();
ShapeBuffer geoR = new ShapeBuffer();
String shpfile = "/Users/lzugis/Documents/chinadata/capital.shp";
String buffile = "/Users/lzugis/Documents/chinadata/capital_buffer.shp";
try{
//读取shp文件
File file = new File(shpfile);
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();
//创建shape文件对象
File fileBuf = new File(buffile);
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put( ShapefileDataStoreFactory.URLP.key, fileBuf.toURI().toURL() );
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
SimpleFeatureType sft = featureSource.getSchema();
List<AttributeDescriptor> attrs = sft.getAttributeDescriptors();
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
for(int i=0;i<attrs.size();i++){
AttributeDescriptor attr = attrs.get(i);
String fieldName = attr.getName().toString();
if(fieldName=="the_geom"){
tb.add(fieldName, Polygon.class);
}
else{
tb.add(fieldName, String.class);
}
}
ds.createSchema(tb.buildFeatureType());
//设置编码
ds.setCharset(charset);
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
while (itertor.hasNext())
{
SimpleFeature feature = itertor.next();
SimpleFeature featureBuf = writer.next();
featureBuf.setAttributes(feature.getAttributes());
Geometry geo = (Geometry)feature.getAttribute("the_geom");
Geometry geoBuffer = geoR.calBuffer(geo, 1.5);
featureBuf.setAttribute("the_geom", geoBuffer);
}
writer.write();
writer.close();
itertor.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("共耗时"+(System.currentTimeMillis() - start)+"ms");
}
}
---------------------------------------------------------------------------------------------------------------技术博客CSDN:http://blog.csdn.NET/gisshixisheng博客园:http://www.cnblogs.com/lzugis/
在线教程http://edu.csdn.Net/course/detail/799Githubhttps://github.com/lzugis/联系方式q q:1004740957e-mail:niujp08@qq.com公众号:lzugis15Q Q 群:452117357(webgis) 337469080(Android)
相关文章推荐
- 用GeoTools实现shp+sld导出图片
- 用GeoTools实现shp+sld导出图片
- hadoop项目:社交粉丝,共同好友数据分析实现
- ActiveReports 报表控件V12新特性 -- 无需ETL处理,即可实现跨数据源分析数据
- Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取
- Rsync+Inotify-tools实现数据实时同步
- 微信小程序数据分析之自定义分析的实现
- 数据挖掘:Apriori 关联规则分析算法原理分析与代码实现
- ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径
- Linux下Rsync+Inotify-tools实现数据实时同步
- PostGIS+QGIS+GeoServer+OpenLayers实现数据的存储、服务的发布以及地图的显示
- 数据分析平台 js 具体实现整理
- 【数据分析 R语言实战】学习笔记 第八章 方差分析与R实现
- BlogEngine.Net架构与源代码分析系列part3:数据存储——基于Provider模式的实现
- linux路由内核实现分析(二)---FIB相关数据结构(2)
- jQuery实现获取form表单内容及绑定数据到form表单操作分析
- OpenLayers项目分析(四)空间数据的组织与实现
- 大数据Spark“蘑菇云”行动第55课:在线广告点击流处理代码的分析和实现
- 利用Python进行数据分析_python3实现_pandas入门_相关系数与协方差
- hadoop单线程实现server多socket连接读取数据原理分析