Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

geotools学习之读取shape文件和创建shape文件


最近两周一直在看geotools,经过两周的学习对geotools有了一个大概的认识,能做做一些基本的业务分析,说到geotools就让我想到arcgis的AE,记得去年我研究AE整整用了两个月的时间才有一点认识,arcgis系列的开发体系完整而且庞大,学习成本较高,在这里主要写一篇关于shape文件的读取和创建,关于里面一些包的引用,对代码做一些简单的注解。这里我用的geotools版本是20。说个坑关于import org.locationtech.jts.geom.Geometry;和import com.vividsolutions.jts.geom.Geometry;前者是新版本所采用的JTS,后者是老版本采用的JTS,你要采用像geotools的20版本,用前者,用geotool的18版本以前的用后者,否则会报错。

一、shape文件读取

1、主要的引用

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;

2、获取feature代码

            //shape文件路径
            String shpfile = "C:/Users/lenovo/Desktop/shape/locations1.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];
            System.out.println(typeName);
            SimpleFeatureSource featureSource = null;
            //根据图层名称来获取要素的source
            featureSource = shpDataStore.getFeatureSource (typeName);
            //获取要素集
            SimpleFeatureCollection result = featureSource.getFeatures();
            //获取要素集合,方便进行迭代读取每一个要素
            SimpleFeatureIterator itertor = result.features(); 

3、关于要素集合迭代代码示例

             //循环读取feature,itertor.hasNext()表示游标下一个是否有数据,有返回ture,否则为false               
            while (itertor.hasNext())
            {
                //获取每一个要素
                SimpleFeature feature = itertor.next();

            } 

二、创建shape文件

1、主要的引用

import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;

2、创建shape文件核心代码

	        //设置要素的字段名称及其类型
	        final SimpleFeatureType TYPE =
	                DataUtilities.createType(
	                        "Location",
	                        "the_geom:Point:srid=4326,"// geometry属性设置
	                                + "name:String,"// 一个字符串属性                               
	                                + "number:Integer" // 数字属性设置
	                        );
                //创建要素集合
	        List<SimpleFeature> features = new ArrayList<>();
                //创建要素模板
	        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
                double latitude = Double.parseDouble(tokens[0]);
	        double longitude = Double.parseDouble(tokens[1]);
	        String name = tokens[2].trim();
	        int number = Integer.parseInt(tokens[3].trim());
	        //创建一个点geometry
	        Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
	        //添加geometry属性
	        featureBuilder.add(point);
	        /添加name属性
	        featureBuilder.add(name);
	        //添加number属性
	        featureBuilder.add(number);
	        //构建要素
	        SimpleFeature feature = featureBuilder.buildFeature(null);
	        //将要素添加到要素几何中
	         features.add(feature);
                	        String shpfile = "C:/Users/lenovo/Desktop/shape/New.shp";
	        File newFile = getNewShapeFile(file);
	        //创建shapefileDataStore工厂
	        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
	        //参数设置
	        Map<String, Serializable> params = new HashMap<>();
	        params.put("url", newFile.toURI().toURL());
	        params.put("create spatial index", Boolean.TRUE);
	        //根据关键字创建shapefileDataStore
	        ShapefileDataStore newDataStore =(ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
               //设置编码,防止中文乱码
               Charset charset = Charset.forName("GBK");
               newDataStore.setCharset(charset);
	       //创建文件描述内容
	       newDataStore.createSchema(TYPE);
               //设置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());
                //获取the_geom属性的值
                Geometry geo =(Geometry) feature.getAttribute("the_geom");
                Geometry geoBuffer = geoR.calBuffer(geo, 3);
                System.out.println(geoBuffer);
                //重新覆盖the_geom属性的值,这里的geoBuffer必须为Geometry类型
                featureBuf.setAttribute("the_geom", geoBuffer);
            } 
            //将所有数据写入
            writer.write();
            //关闭写入流
            writer.close();

转载自:https://blog.csdn.net/weixin_40184249/article/details/83898305