GeoTools——新建shapefile文件

目录

一、引言

二、代码操作

1、服务端

2、返回数据

3、客户端

三、第二种新建shp方法

四、总结


 

 

一、引言

 

本文讲解了如何读取shp数据,如何新建shp数据,并将新建的shp数据返回到客户端进行显示。

 

 

二、代码操作

 

1、服务端

list对象是为了最后将新建的shp数据返回,pointgbk数据是源文件,pointgbkbuffer是新建文件。

    @RequestMapping("/buffer")
    @ResponseBody
    public Object buffer()
    {
        List<Map<String,Object>>list=new ArrayList<>();
        long start = System.currentTimeMillis();

        String shpfile = this.getClass().getResource("/").getFile()+"file/pointgbk.shp";
        String buffile = this.getClass().getResource("/").getFile()+"file/pointgbkbuffer.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);
            //获取原shp文件字段名
            SimpleFeatureType sft = featureSource.getSchema();
            List<AttributeDescriptor> attrs = sft.getAttributeDescriptors();

            //定义图形信息和属性信息
  
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤来统计shapefile文件中地类面积: 1. 使用java.io.FileInputStream打开shapefile文件,读取文件数据。 2. 使用org.geotools.data.shapefile.ShapefileDataStoreFactory创建ShapefileDataStore。 3. 使用ShapefileDataStore的getFeatureSource方法获取FeatureSource。 4. 使用FeatureSource的getFeatures方法获取所有的Feature。 5. 循环遍历所有的Feature,获取每个Feature的面积。可以使用org.geotools.geometry.jts.JTS的st_area方法计算面积。 6. 累加所有Feature的面积,得到shapefile文件中地类面积的总和。 下面是一个示例代码: ```java import java.io.FileInputStream; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geometry.jts.JTS; import com.vividsolutions.jts.geom.Geometry; public class ShapefileAreaExample { public static void main(String[] args) throws Exception { // 打开shapefile文件 FileInputStream inputStream = new FileInputStream("shapefile.shp"); // 创建ShapefileDataStore ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(inputStream); // 获取FeatureSource SimpleFeatureSource featureSource = dataStore.getFeatureSource(); // 获取所有的Feature FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(); // 统计地类面积总和 double areaSum = 0; try (FeatureIterator<SimpleFeature> featureIterator = features.features()) { while (featureIterator.hasNext()) { SimpleFeature feature = featureIterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); double area = JTS.toGeometry(geometry.getEnvelopeInternal()).getArea(); areaSum += area; } } // 输出地类面积总和 System.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值