java 解析shp文件(转GeoJSON\esrijson、生成图片)

1.下载jar包(可以去我的gitee上看源码并下载也可以去geotools管网下载)

java 解析shp文件(转GeoJSON\esrijson、生成图片)_第1张图片

2.根目录下新建lib文件夹,将jar包放入

3.配置maven pom文件



    org.geotools
    gt-shapefile
    19.2
    system
    ${project.basedir}/lib/gt-shapefile-19.2.jar


    org.ejml
    ejml-ddense
    0.39
    system
    ${project.basedir}/lib/ejml-ddense-0.32.jar


    org.ejml
    ejml-core
    0.39
    system
    ${project.basedir}/lib/ejml-core-0.39.jar


    org.geotools
    gt-opengis
    19.2
    system
    ${project.basedir}/lib/gt-opengis-19.2.jar



    org.geotools
    gt-data
    19.2
    system
    ${project.basedir}/lib/gt-data-19.2.jar



    org.geotools
    gt-api
    19.2
    system
    ${project.basedir}/lib/gt-api-19.2.jar



    org.geotools
    gt-main
    19.2
    system
    ${project.basedir}/lib/gt-main-19.2.jar



    org.geotools
    gt-metadata
    19.2
    system
    ${project.basedir}/lib/gt-metadata-19.2.jar



    org.geotools
    gt-referencing
    19.2
    system
    ${project.basedir}/lib/gt-referencing-19.2.jar



    org.geotools
    gt-geojson
    19.2
    system
    ${project.basedir}/lib/gt-geojson-19.2.jar



    com.google.code.gson
    gson
    2.8.5


    org.json.simple
    json-simple
    1.1
    system
    ${project.basedir}/lib/json-simple-1.1.jar


    javax.measure
    jsr-275-1.0-beta
    2
    system
    ${project.basedir}/lib/jsr-275-1.0-beta-2.jar


    com.vividsolutions
    jts
    1.13
    system
    ${project.basedir}/lib/jts-1.13.jar

3.编写工具类

public class ParsingShpFileUtils {

    public static void main(String[] args) throws Exception {
        System.out.println(ParsingShpFile("/Users/wuchao/Documents/bengzhan.shp"));
    }

    /**
     * 解析shp文件
     * @param filePath
     * @return
     * @throws Exception
     */
    public static Map ParsingShpFile(String filePath) throws Exception {
        File file = new File(filePath);
        if (!file.exists()){
            throw new Exception("文件不存在!");
        }
        if (!filePath.endsWith("shp")){
            throw new Exception("只能指定后缀为shp的文件");
        }
        Map map = new HashMap();
        List list = new ArrayList();
        //读取shp
        SimpleFeatureCollection colls1 = readShp(filePath);
        SimpleFeatureType schema = colls1.getSchema();
        Name name = schema.getGeometryDescriptor().getType().getName();
        ReferencedEnvelope bounds = colls1.getBounds();
        //所有features
        SimpleFeatureIterator iters = colls1.features();
        String s = name.toString();
        if ("Point".equals(s)) {
            list = parsingPoint(iters);
        } else if ("MultiLineString".equals(s) || "MultiPolygon".equals(s)) {
            list = parsingLineOrPoly(iters);
        }
        map.put("data",list);
        map.put("maxX",bounds.getMaxX());
        map.put("minX",bounds.getMinX());
        map.put("maxY",bounds.getMaxY());
        map.put("minY",bounds.getMinY());
        map.put("shapeFile",name.toString());
        return map;
    }
    /**
     * 解析点数据
     *
     * @param iters
     * @return
     */
    public static List parsingPoint(SimpleFeatureIterator iters) {
        List list = new ArrayList();
        while (iters.hasNext()) {
            SimpleFeature sf = iters.next();
            Map map = new HashMap();
            Iterator iterator = sf.getValue().iterator();
            while (iterator.hasNext()) {
                Property property = iterator.next();
                if (property.getValue() instanceof Point) {
                    map.put("PointX", ((Point)(property.getValue())).getX());
                    map.put("PointY", ((Point)(property.getValue())).getY());
                }else{
                    Name name = property.getName();//属性名称
                    Object value = property.getValue();//属性值
                    map.put(name,value);
                }
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 解析线和面
     *
     * @param iters
     * @return
     */
    public static List parsingLineOrPoly(SimpleFeatureIterator iters) {
        List list = new ArrayList();
        while (iters.hasNext()) {
            SimpleFeature sf = iters.next();
            Map map = new HashMap();
            Iterator iterator = sf.getValue().iterator();
            while (iterator.hasNext()) {
                Property property = iterator.next();
                if (property.getValue() instanceof Geometry) {
                    Geometry geometry = (Geometry) property.getValue();
                    Coordinate[] coordinates = geometry.getCoordinates();
                    List paths = new ArrayList();
                    for (Coordinate coordinate : coordinates) {
                        Map path = new HashMap();
                        path.put("x",coordinate.x);
                        path.put("y",coordinate.y);
                        path.put("z",coordinate.z);
                        paths.add(path);
                    }
                    map.put("path",paths);
                }else{
                    Name name = property.getName();//属性名称
                    Object value = property.getValue();//属性值
                    map.put(name,value);
                }
            }
            list.add(map);
        }
        return list;
    }

    public static SimpleFeatureCollection readShp(String path) {
        return readShp(path, null);

    }

    public static SimpleFeatureCollection readShp(String path, Filter filter) {
        SimpleFeatureSource featureSource = readStoreByShp(path);
        if (featureSource == null) return null;
        try {
            return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SimpleFeatureSource readStoreByShp(String path) {
        File file = new File(path);
        FileDataStore store;
        SimpleFeatureSource featureSource = null;
        try {
            store = FileDataStoreFinder.getDataStore(file);
            ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));
            featureSource = store.getFeatureSource();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return featureSource;
    }
}

4.gitee 地址:java-geotools: java语言使用geotools工具解决相关问题

目前更新的功能有:

  1. 解析shp 点线面文件 获取数据详情
  2. shp数据转成GeoJSON
  3. shp文件转esrijson,逻辑上是先将shp转成GeoJSON文件,再将GeoJSON转成esrijson
  4. 主要功能是将GeoJSON生成图片,同理shp也可以先转GeoJSON再生成图片。这个功能主要解决了geotools工具在处理数据时properties属性不一致问题(不过多描述)
  5. 2023-03-21新增根据shp文件直接生成图片代码

你可能感兴趣的:(share,java,gis,shapefile)