GeoTools学习(一)读取shapefile文件

本文参考文章:https://blog.csdn.net/superbeyone/article/details/82799645https://blog.csdn.net/xcymorningsun/article/details/89846280

1.配置Maven仓库地址

 <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>boundless</id>
            <name>Boundless Maven Repository</name>
            <url>http://repo.boundlessgeo.com/main</url>
        </repository>
</repositories>

2.引入pom依赖

<dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-shapefile</artifactId>
      <version>18.4</version>
</dependency>

3.创建与shape文件映射的实体类

这一部分的实体类应与自己shp文件中的属性一致,如下图所示,我的shp文件的属性为FID,Shape,id....,实体类的创建这些属性相对应,在读取shp文件时,存储对应的属性

创建的实体类如下所示

package geotools.Shape;


/**
 * @program: tdt-commons
 * @description: Shape文件映射
 * @author: Mr.superbeyone
 * @create: 2018-09-19 11:04
 **/

public class ShapeModel {
	private String FID;

    private String Shape;

    //以此类推,不列出所有的属性
    /**
    *Get和Set方法
    */
    public String getFID() {

    	  return FID;

    	}
    public void setFID(String FID) {

  	  this.FID= FID;

  	}
    public String getShape() {

  	  return Shape;

  	}
  public void setShape(String Shape) {

	  this.Shape= Shape;

	}
  
}

4.创建读取ShapeFile的工具类

从本地读取shapefile文件,得到featurecollection,得到feature,遍历feature的每个property,property就是shp文件的所有字段,通过遍历可以得到要素集所有要素的属性,property.getName().toString()获得字段名称,在属性表表现为字段名称,如下图中的FID,Shape等;property.getValue().toString()为字段值,如下图中的1,面等。

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @program: commons
 * @description: 读取Shp文件
 * @author: Mr.superbeyone
 * @create: 2018-09-19 15:26
 **/
public class ShapeReader {

    public ArrayList<ShapeModel> readShapeFile(String filePath) {
        ArrayList<ShapeModel> modelList = new ArrayList<>();
        File folder = new File(filePath);
        if (!folder.isDirectory()) {
            if (folder.toString().endsWith(".shp")) {
                try {
                    modelList = getShapeFile(folder);
                    return modelList;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("选择的文件后缀名不是.shp");
            }
        }else{
	        File[] files = folder.listFiles();
	        if (!(files.length > 0)) {
	            System.out.println("目录文件为空");
	            return modelList;
	        }
	
	        for (File file : files) {
	            if (!file.toString().endsWith(".shp")) {
	                continue;
	            }
	            try {
	                ArrayList<ShapeModel> models = getShapeFile(file);
	                modelList.addAll(models);
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
        }
        return modelList;
    }

    private ArrayList<ShapeModel> getShapeFile(File file) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", file.toURI().toURL());
        ArrayList<ShapeModel> models = new ArrayList<>();
        DataStore dataStore = DataStoreFinder.getDataStore(map);
        //字符转码,防止中文乱码
        ((ShapefileDataStore) dataStore).setCharset(Charset.forName("utf8"));
        String typeName = dataStore.getTypeNames()[0];
        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
        FeatureIterator<SimpleFeature> features = collection.features();
        while (features.hasNext()) {
            SimpleFeature feature = features.next();
            ShapeModel model = new ShapeModel();
            Iterator<? extends Property> iterator = feature.getValue().iterator();
            while (iterator.hasNext()) {            
                Property property = iterator.next();
                //property数据与实体类对应
                if (property.getName().toString().equals("FID"))
                    model.setGeoStr(property.getValue().toString());
                if (property.getName().toString().equals("Shape"))
                    model.setLayer(property.getValue().toString());

            }
            models.add(model);
        }
        return models;
    }

}

5.使用详解

//此处传值既可以是一个目录也可以是一个以.shp结尾的文件,如果是目录的话,会遍历读取所有的以.shp结尾的文件
String filePath = ""; 
ShapeReader shapeReader = new ShapeReader();
shapeReader.readShapeFile(filePath);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值