geotools實現shp數據的緩衝區分析

概述:java

本文講述如何在geotools中實現shp數據的緩衝區分析並保存到shp文件中。git


效果:github


實現代碼:web

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");
	}
}

---------------------------------------------------------------------------------------------------------------api

技術博客ssh

CSDN:http://blog.csdn.NET/gisshixishengui

博客園:http://www.cnblogs.com/lzugis/
編碼

在線教程spa

http://edu.csdn.Net/course/detail/799.net

Github

https://github.com/lzugis/

聯繫方式

q       q:1004740957

e-mail:niujp08@qq.com

公衆號:lzugis15

Q Q 羣:452117357(webgis)

             337469080(Android)

相關文章
相關標籤/搜索