GeoTools应用:读取Shape文件中的空间坐标数据(2)


一、环境准备

装配GeoTools有两种方式,一种是配置maven工程的pom文件(配置方式参考官网),另一种是下载geotools的jar包到本地导入依赖。我采用的是下载jar的方式,下载路径:https://sourceforge.net/projects/geotools/files/

二、实现功能

本章要实现的功能是从shape文件中读取空间坐标数据,包括点、多线、多多边形数据的读取。基本上涵盖了实际项目中用到的所有地图数据。

三、样例代码

1、构建基础GIS对象模型

直接使用GeoTools的geometry模型,不好用。实际应用中都会在外面包一层,具体封装什么根据业务需要来。这里给一个简单的样例,后面的所有样例代码都会沿用这些模型。

1)定义对象类型枚举值

package com.elon.constant;

/**
 * GIS图层对象枚举定义
 * @author elon
 * @version 2018年6月26日
 */
public enum EnumGISObjectType {
    POINT(1),

    LINE(2),

    POLYGON(4);

    private int type;

    private EnumGISObjectType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }
}

2)定义GISObjectBase.java基类

package com.elon.model.gismodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opengis.feature.simple.SimpleFeature;

import com.elon.constant.EnumGISObjectType;
import com.elon.model.ShapeFieldInfo;

/**
 * GIS对象基类定义。
 * 
 * @author elon
 * @version 2018年6月26日
 */
public class GISObjectBase implements Serializable {

    private static final long serialVersionUID = -6147262367078689317L;

    /**
     * 对象类型枚举
     */
    private final EnumGISObjectType type;

    private SimpleFeature simpleFeature = null;

    /**
     * 属性列信息
     */
    private List<ShapeFieldInfo> attrFieldList = new ArrayList<>();

    /**
     * 属性值信息<属性名称, 属性值>
     */
    private Map<String, Object> attributeMap = new HashMap<>();

    protected GISObjectBase(EnumGISObjectType type, SimpleFeature simpleFeature,
            List<ShapeFieldInfo> attrFieldList){
        this.type = type;
        this.simpleFeature = simpleFeature;
        this.attrFieldList = attrFieldList;
    }

    public EnumGISObjectType getType() {
        return type;
    }

    public SimpleFeature getSimpleFeature() {
        return simpleFeature;
    }

    public Map<String, Object> getAttributeMap() {
        return attributeMap;
    }

    public void setAttributeMap(Map<String, Object> attributeMap) {
        this.attributeMap = attributeMap;
    }

    public void addAttribute(String attrName, Object value) {
        attributeMap.put(attrName, value);
    }

    public void setSimpleFeature(SimpleFeature simpleFeature) {
        this.simpleFeature = simpleFeature;
    }

    public List<ShapeFieldInfo> getAttrFieldList() {
        return attrFieldList;
    }

    public void setAttrFieldList(List<ShapeFieldInfo> attrFieldList) {
        this.attrFieldList = attrFieldList;
    }
}

3)定义点模型GISPoint.java*

package com.elon.model.gismodel;

import org.opengis.feature.simple.SimpleFeature;

import com.elon.constant.EnumGISObjectType;
import com.vividsolutions.jts.geom.Point;

/**
 * 点对象模型
 * @author elon
 * @version 2018年6月26日
 */
public class GISPoint extends GISObjectBase {

    private static final long serialVersionUID = 851468237977190995L;

    /**
     * 点geometry对象
     */
    private Point point = null;

    public GISPoint(Point point, SimpleFeature simpleFeature) {
        super(EnumGISObjectType.POINT, simpleFeature);
        this.point = point;
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }
}

4)定义线模型GisLine.java

package com.elon.model.gismodel;

import org.opengis.feature.simple.SimpleFeature;

import com.elon.constant.EnumGISObjectType;
import com.vividsolutions.jts.geom.MultiLineString;

/**
 * 线对象模型。
 * 
 * @author elon
 * @version 2018年6月26日
 */
public class GisLine extends GISObjectBase {

    private static final long serialVersionUID = 495559767188836052L;

    /**
     * 多线geometry对象
     */
    private MultiLineString line = null;

    public GisLine(MultiLineString line, SimpleFeature simpleFeature) {
        super(EnumGISObjectType.LINE, simpleFeature);
        this.line = line;
    }

    public MultiLineString getLine() {
        return line;
    }

    public void setLine(MultiLineString line) {
        this.line = line;
    }
}

5)定义多边形模型

package com.elon.model.gismodel;

import org.opengis.feature.simple.SimpleFeature;

import com.elon.constant.EnumGISObjectType;
import com.vividsolutions.jts.geom.MultiPolygon;

/**
 * 多边形模型。
 * 
 * @author elon
 * @version 2018年6月26日
 */
public class GisMultiPolygon extends GISObjectBase {

    private static final long serialVersionUID = -5705724544971923893L;

    /**
     * 多边形的geomtry模型。
     */
    private MultiPolygon polygon = null;;

    public GisMultiPolygon(MultiPolygon polygon, SimpleFeature simpleFeature) {
        super(EnumGISObjectType.POLYGON, simpleFeature);
        this.polygon = polygon;
    }

    public MultiPolygon getPolygon() {
        return polygon;
    }

    public void setPolygon(MultiPolygon polygon) {
        this.polygon = polygon;
    }
}

2、读取GIS对象代码

package com.elon.shape;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
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 org.opengis.feature.type.AttributeDescriptor;

import com.elon.model.ShapeFieldInfo;
import com.elon.model.gismodel.GISObjectBase;
import com.elon.model.gismodel.GISPoint;
import com.elon.model.gismodel.GisLine;
import com.elon.model.gismodel.GisMultiPolygon;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;

/**
 * Shape文件操作公共类。
 * @author elon
 * @version 2018年6月24日
 */
public class ShapeUtils {    
    /**
     * 读取GIS图层对象。
     * @param shpFilePath shp文件路径
     * @return 对象列表
     */
    @SuppressWarnings("unchecked")
    public static <T extends GISObjectBase> List<T> readGisObject(String shpFilePath) {
        List<T> gisObjectList = new ArrayList<>();
        ShapefileDataStore dataStore = buildDataStore(shpFilePath);

        try {
            String typeName = dataStore.getTypeNames()[0];
            FeatureSource<SimpleFeatureType, SimpleFeature> fs = dataStore.getFeatureSource(typeName);
            FeatureCollection<SimpleFeatureType, SimpleFeature> fcResult = fs.getFeatures();
            System.out.println("fcResult size:" + fcResult.size());

            FeatureIterator<SimpleFeature> iter = fcResult.features();
            while (iter.hasNext()) {
                SimpleFeature sf = iter.next();
                Collection<Property> property = sf.getProperties();
                Iterator<Property> iterP = property.iterator();
                while (iterP.hasNext()) {
                    Property pro = iterP.next();

                    if (pro.getValue() instanceof MultiPolygon) {
                        gisObjectList.add((T) new GisMultiPolygon((MultiPolygon) pro.getValue(), sf));
                    } else if (pro.getValue() instanceof Point) {
                        gisObjectList.add((T) new GISPoint((Point) pro.getValue(), sf));
                    } else if (pro.getValue() instanceof MultiLineString) {
                        gisObjectList.add((T) new GisLine((MultiLineString) pro.getValue(), sf));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dataStore.dispose();
        }

        return gisObjectList;
    }

    /**
     * 构建ShapeDataStore对象。
     * @param shpFilePath shape文件路径。
     * @return
     */
    public static ShapefileDataStore buildDataStore(String shpFilePath) {
        ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
        try {
            ShapefileDataStore dataStore = (ShapefileDataStore) factory
                    .createDataStore(new File(shpFilePath).toURI().toURL());
            if (dataStore != null) {
                dataStore.setCharset(Charset.forName("UTF-8"));
            }
            return dataStore;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

3、客户端调用代码

package com.elon;

import java.io.File;
import java.util.List;

import com.elon.model.gismodel.GisMultiPolygon;
import com.elon.shape.ShapeUtils;

public class StartupGeoTools {
    public static void main(String[] args) {

        String workSpacePath = System.getProperty("user.dir");
        String shpFilePath = workSpacePath + File.separator + "shape/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp";

        List<GisMultiPolygon> gisObjectList = ShapeUtils.readGisObject(shpFilePath);
        System.out.println("gisObjectList size:" + gisObjectList.size());

        System.out.println("Start GeoTools success!");
    }
}

转载自:https://blog.csdn.net/ylforever/article/details/80813711

You may also like...