java导入shp数据并解析

通过java导入shp数据并将数据解析成集合

首先需要引入shp解析使用的jar包,下载需要“外”网环境(你懂得),我这里直接下载使用的本地包(主要是懒,你也可以搜索下:怎么将jar包导入maven仓库中,很简单)


http://123.​​​​​​207.0.33:8009/environmentProtectiong/file/lib.rarhttp://123.​​​​​​207.0.33:8009/environmentProtectiong/file/lib.rar网盘没会员实在太慢了,直接自己连接发你们

下载后直接把lib放到项目目录

 <!--		shp解析-->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-shapefile-19.2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.ejml</groupId>
            <artifactId>ejml-ddense</artifactId>
            <version>0.39</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ejml-ddense-0.32.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.ejml</groupId>
            <artifactId>ejml-core</artifactId>
            <version>0.39</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ejml-core-0.39.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-opengis-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-data</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-data-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-api</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-api-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-main-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-metadata</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-metadata-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-referencing</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-referencing-19.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>19.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gt-geojson-19.2.jar</systemPath>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.json.simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/json-simple-1.1.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>javax.measure</groupId>
            <artifactId>jsr-275-1.0-beta</artifactId>
            <version>2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/jsr-275-1.0-beta-2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.13</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/jts-1.13.jar</systemPath>
        </dependency>

使用本地jar包打包的时候要加

<configuration>
   <includeSystemScope>true</includeSystemScope>
</configuration>

不然打完的包访问不到对应jar包资源

解析util类:

 public Map<String,Object> getShpFileAttributes(File shpFile)  {
        Map<String,Object> map = new HashMap();
        JSONArray jsonArray = new JSONArray();
        List<String> attributeID = new LinkedList();
        try {
            FileDataStore store = FileDataStoreFinder.getDataStore(shpFile);
            // GeoTools读取ShapeFile文件的默认编码为ISO-8859-1。而我们中文操作系统下ShapeFile 
             文件的默认编码一般为utf-8
            ((ShapefileDataStore) store).setCharset(Charset.forName("utf-8"));
            SimpleFeatureSource featureSource = store.getFeatureSource();
            // 提取出属性的 ID 值

            List<AttributeDescriptor> attrList= 
                            featureSource.getSchema().getAttributeDescriptors();
            for(AttributeDescriptor attr : attrList){
                String ID = attr.getName().getLocalPart();
                    attributeID.add(ID);
            }
            SimpleFeatureCollection featureCollection = featureSource.getFeatures();
            SimpleFeatureIterator featureIterator = featureCollection.features();

            while (featureIterator.hasNext()) {
                SimpleFeature feature = featureIterator.next();
                JSONObject jsonObject = new JSONObject();
                List list = feature.getAttributes();
                for(int k = 0; k < list.size(); k++ ){
                    if (list.get(k)!=null){
                        jsonObject.put(attributeID.get(k), list.get(k).toString());
                    }else{
                        jsonObject.put(attributeID.get(k), null);
                    }
                }
                jsonArray.add(jsonObject);
                jsonObject = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }
        map.put("data", jsonArray);
        map.put("shpName", attributeID);
        return map;
    }

虽然他解析的只是后缀为shp的文件但是shp的同级文件必不可少至少要包含四个文件:

文件越全越好,保证这些文件在同一目录下, 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值