Java使用GeoTools读写shp文件

24 篇文章 5 订阅
6 篇文章 0 订阅

根据geotools自己封装的工具类,主要有shp文件的内容读取,转geojson,要素的增删改,新shp生成

需要的依赖

这里主要用的是geotools的依赖,版本是<geotools.version>23.2</geotools.version>
还用到了fastjson,版本1.2.57

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.57</version>
</dependency>
<!-- geotools -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-geojson</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-cql</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-ysld</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-hsql</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-extension</artifactId>
    <version>${geotools.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-main -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-opengis</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-xml</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>${geotools.version}</version>
</dependency>

如果不好下需要指定一下repository

<repository>
    <id>GeoSolutions</id>
    <url>http://maven.geo-solutions.it/</url>
</repository>

读取工具

public class ShapeFileReaderUtils {

    /**
     * 校验shp文件
     *
     * @param fileUrl 文件地址 shp或shp文件夹
     * @return File
     * @throws Exception e
     */
    public static File checkShapeFile(String fileUrl) throws Exception {
        File file = new File(fileUrl);
        if (file.isDirectory()) {
            File[] fa = file.listFiles();
            if (fa == null || fa.length < 1) {
                throw new Exception("找不到shp文件");
            }
            boolean flag = true;
            for (File f : fa) {
                if (new ShpFiles(file).exists(ShpFileType.SHP)) {
                    file = f;
                    flag = false;
                    break;
                }
            }
            if (flag) {
                throw new Exception("找不到shp文件");
            }
        } else {
            if (!new ShpFiles(file).exists(ShpFileType.SHP)) {
                throw new Exception("找不到shp文件");
            }
        }
        return file;
    }

    /**
     * shp文件要素源
     *
     * @param fileUrl 文件地址
     * @return ContentFeatureSource
     * @throws Exception e
     */
    public static ContentFeatureSource getFeatureSourceFromShapeFile(String fileUrl) throws Exception {
        ShapefileDataStore sds = null;
        try {
            ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
            File file = checkShapeFile(fileUrl);

            sds = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());
            String type = sds.getTypeNames()[0];
            sds.setCharset(getShapeFileCharsetName(fileUrl));

            return sds.getFeatureSource(type);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            assert sds != null;
            sds.dispose();
        }
    }

    /**
     * 获取要素几何类型
     *
     * @param featureSource 要素源
     * @return 几何类型
     */
    public static String getGeometryTypeName(SimpleFeatureSource featureSource) {
        SimpleFeatureType schema = featureSource.getSchema();
        return schema.getGeometryDescriptor().getType().getName().toString();
    }

    /**
     * shp转geoJsonArray
     *
     * @param fileUrl 文件地址
     * @return JSONArray
     */
    public static JSONArray shapeFileToJsonArray(String fileUrl) throws IOException {
        JSONArray array = new JSONArray();
        FeatureJSON featureJSON = new FeatureJSON();
        FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = null;
        try {
            ContentFeatureSource featureSource = getFeatureSourceFromShapeFile(fileUrl);
            assert featureSource != null;
            featureReader = featureSource.getReader();
            while (featureReader.hasNext()) {
                SimpleFeature feature = featureReader.next();
                StringWriter writer = new StringWriter();
                featureJSON.writeFeature(feature, writer);
                JSONObject json = JSONObject.parseObject(String.valueOf(writer));
                array.add(json);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (featureReader != null) {
                featureReader.close();
            }
        }

        return array;
    }

    /**
     * shp转geoJsonArray
     *
     * @param fileUrl shp文件地址
     * @param filter  Filter 参考https://docs.geotools.org/latest/userguide/library/main/filter.html
     * @return JSONArray
     */
    public static JSONArray shapeFileToJsonArray(String fileUrl, Filter filter) throws IOException {
        JSONArray array = new JSONArray();
        FeatureJSON featureJSON = new FeatureJSON();
        FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = null;
        try {
            ContentFeatureSource featureSource = getFeatureSourceFromShapeFile(fileUrl);
			assert featureSource != null;
            featureReader = featureSource.getReader(filter);
            while (featureReader.hasNext()) {
                SimpleFeature feature = featureReader.next();
                StringWriter writer = new StringWriter();
                featureJSON.writeFeature(feature, writer);
                JSONObject json = JSONObject.parseObject(String.valueOf(writer));
                array.add(json);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (featureReader != null) {
                featureReader.close();
            }
        }

        return array;
    }

    /**
     * 获取shp几何对象集合
     *
     * @param fileUrl 文件地址
     * @return 几何集合
     */
    public static List<Geometry> getGeometryList(String fileUrl) throws IOException {
        List<Geometry> geometryList = new ArrayList<>();
        ShapefileReader r = null;
        try {
            r = new ShapefileReader(new ShpFiles(fileUrl), false, false, new GeometryFactory());
            while (r.hasNext()) {
                Geometry shape = (Geometry) r.nextRecord().shape();
                geometryList.add(shape);
            }
        } catch (Exception e) {
            if (r != null) {
                r.close();
            }
            e.printStackTrace();
        }

        return geometryList;
    }

    /**
     * 带过滤器的获取shp几何方法
     * 使用FeatureReader而不是ShapefileReader,效率可能会低一些
     *
     * @param fileUrl shp文件地址
     * @param filter  Filter 参考https://docs.geotools.org/latest/userguide/library/main/filter.html
     * @return List<Geometry>
     */
    public static List<Geometry> getGeometryList(String fileUrl, Filter filter) throws IOException {
        List<Geometry> geometryList = new ArrayList<>();
        FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = null;

        try {
            ContentFeatureSource featureSource = getFeatureSourceFromShapeFile(fileUrl);
            assert featureSource != null;
            featureReader = featureSource.getReader(filter);
			
            //遍历featureCollection
            while (featureReader.hasNext()) {
                SimpleFeature feature = featureReader.next();
                Geometry geom = (Geometry) feature.getDefaultGeometry();
                geometryList.add(geom);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (featureReader != null) {
                featureReader.close();
            }
        }

        return geometryList;
    }

    /**
     * 读取shp属性表
     *
     * @param path dbf路径
     * @return JSONArray
     */
    public static JSONArray getAttributesList(String path) {
        JSONArray array = new JSONArray();
        JSONObject object = null;
        Charset charSet = getShapeFileCharsetName(path);
        // try()的括号中可以写多行声明,每个声明的变量类型都必须是Closeable的子类,用分号隔开,自动close
        try (DbaseFileReader reader = new DbaseFileReader(new ShpFiles(path), false, charSet)) {
            DbaseFileHeader header = reader.getHeader();
            int numFields = header.getNumFields();
            //迭代读取记录
            while (reader.hasNext()) {
                try {
                    Object[] entry = reader.readEntry();
                    object = new JSONObject();
                    for (int i = 0; i < numFields; i++) {
                        String title = header.getFieldName(i);
                        Object value = entry[i];
                        object.put(title, value);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                array.add(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭
        return array;
    }

    /**
     * shp转geoJsonObject
     *
     * @param fileUrl 文件地址
     * @return JSONObject
     */
    public static JSONObject ShapeFileToGeoJsonObject(String fileUrl) throws IOException {
        JSONObject res = new JSONObject();
        res.put("type", "FeatureCollection");
        res.put("features", shapeFileToJsonArray(fileUrl));
        return res;
    }

    public static JSONObject ShapeFileToGeoJsonObject(String fileUrl, Filter filter) throws IOException {
        JSONObject res = new JSONObject();
        res.put("type", "FeatureCollection");
        res.put("features", shapeFileToJsonArray(fileUrl, filter));
        return res;
    }

    /**
     * 高版本arcgis生成的shp会有一个cpg文件记录编码格式
     *
     * @param path shp路径
     * @return 编码格式
     */
    static Charset getShapeFileCharsetName(String path) {
        path = path.replaceAll(".shp", ".cpg");
        File pFile = new File(path);
        String encode = "GBK";
        if (!pFile.exists() || !pFile.isFile()) {
            return Charset.forName(encode);
        }
        File file = new File(path);

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String tempString;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                if ("UTF-8".equals(tempString.toUpperCase())) {
                    encode = "UTF-8";
                    break;
                }
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Charset.forName(encode);
    }

}

写入工具

public class ShapeFileWriterUtils {

    /**
     * 单个过滤器的要素移除
     *
     * @param match Filter 参考https://docs.geotools.org/latest/userguide/library/main/filter.html
     * @param path  shp路径
     * @throws IOException e
     */
    public static void removeFeatures(Filter match, String path) throws Exception {
        ShapefileDataStore shapefileDataStore = getShpDataStore(path);
        String typeName = shapefileDataStore.getTypeNames()[0];
        SimpleFeatureStore dataStore = (SimpleFeatureStore) shapefileDataStore.getFeatureSource(typeName);
        Transaction transaction = new DefaultTransaction("remove");
        dataStore.setTransaction(transaction);
        try {
            dataStore.removeFeatures(match);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            transaction.close();
        }
    }

    /**
     * 多个过滤器的要素移除 单纯记录下FilterFactory2用法,建议条件自己写成单个的
     *
     * @param match Filter 参考https://docs.geotools.org/latest/userguide/library/main/filter.html
     * @param path  shp路径
     * @throws IOException e
     */
    public static void removeFeatures(List<Filter> match, String path) throws Exception {
        // 多个filter可以这样拼成一个
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        Filter filter = ff.or(match);
        Filter filterAnd = ff.and(match);

        removeFeatures(filter, path);
    }

    /**
     * 修改shp文件要素的属性
     *
     * @param attributeName  属性字段名
     * @param attributeValue 属性值
     * @param filter         查询filter
     * @param path           文件路径
     * @throws IOException e
     */
    public static void modifyFeatures(String[] attributeName, Object[] attributeValue, Filter filter, String path) throws Exception {
        ShapefileDataStore shapefileDataStore = getShpDataStore(path);
        Transaction transaction = new DefaultTransaction("modify");
        String typeName = shapefileDataStore.getTypeNames()[0];
        SimpleFeatureStore dataStore = (SimpleFeatureStore) shapefileDataStore.getFeatureSource(typeName);
        dataStore.setTransaction(transaction);
        try {
            dataStore.modifyFeatures(attributeName, attributeValue, filter);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
        }
    }

    /**
     * 给现有shp增加要素
     *
     * @param featureCollection SimpleFeatureCollection
     * @param path              shp路径
     * @throws Exception e
     */
    public static void appendFeature(SimpleFeatureCollection featureCollection, String path) throws Exception {
        // 创建输出数据存储
        ShapefileDataStore shpDataStore = getShpDataStore(path);
        SimpleFeatureType inputSchema = featureCollection.getSchema();
        SimpleFeatureType dataStoreSchema = shpDataStore.getSchema();
        if (FeatureTypes.equals(inputSchema, dataStoreSchema)) {
            throw new Exception("error! featureCollection schema not equals to dataStore schema!");
        }

        addFeatureTransaction(shpDataStore, featureCollection);
    }

    /**
     * 生成shp
     *
     * @param featureCollection SimpleFeatureCollection
     * @param path              输出路径 shp结尾
     * @throws IOException e
     */
    public static void buildShp(SimpleFeatureCollection featureCollection, String path) throws Exception {
        // 创建输出数据存储
        ShapefileDataStore newDataStore = getShpDataStore(path);
        SimpleFeatureType type = featureCollection.getSchema();

        // 设置输出数据存储
        newDataStore.createSchema(type);
        // 设置坐标系
        newDataStore.forceSchemaCRS(type.getCoordinateReferenceSystem());

        addFeatureTransaction(newDataStore, featureCollection);
    }

    /**
     * geoJson文件转shp
     *
     * @param geoJson geoJson文件
     * @param path    文件输出地址 .shp结尾
     * @throws IOException e
     */
    public static void geoJSON2Shp(File geoJson, String path) throws Exception {
        InputStream in = new FileInputStream(geoJson);
        geoJSON2Shp(in, path);
    }

    /**
     * geoJson转shp 流式
     *
     * @param input geoJson流
     * @param path  文件输出地址 .shp结尾
     * @throws IOException e
     */
    public static void geoJSON2Shp(InputStream input, String path) throws Exception {

        FeatureJSON fjson = new FeatureJSON(new GeometryJSON(15));

        FeatureIterator<SimpleFeature> jsonIt = fjson.streamFeatureCollection(input);

        if (!jsonIt.hasNext()) {
            throw new IllegalArgumentException(
                    "Cannot create shapefile. GeoJSON stream is empty");
        }

        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;

        try {
            // 创建输出数据存储
            ShapefileDataStore shpDataStore = getShpDataStore(path);

            // use feature type of first feature
            SimpleFeature firstFeature = jsonIt.next();
            shpDataStore.createSchema(firstFeature.getFeatureType());

            writer = shpDataStore.getFeatureWriterAppend(
                    shpDataStore.getTypeNames()[0], Transaction.AUTO_COMMIT);

            addFeature(firstFeature, writer);

            while (jsonIt.hasNext()) {
                SimpleFeature feature = jsonIt.next();
                addFeature(feature, writer);
            }
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * 若文件存在,返回该shp;若不存在,创建新dataStore
     *
     * @param path 文件路径
     * @return ShapefileDataStore
     * @throws IOException e
     */
    private static ShapefileDataStore getShpDataStore(String path) throws Exception {
        File newFile = new File(path);
        ShapefileDataStore inputDataStore;
        if (newFile.exists()) {
            inputDataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(
                    Collections.singletonMap("url", newFile.toURI().toURL()));
        } else {
            ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
            inputDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(
                    Collections.singletonMap("url", newFile.toURI().toURL()));
        }
        inputDataStore.setCharset(getShapeFileCharsetName(path));
        return inputDataStore;
    }

    private static void addFeatureTransaction(ShapefileDataStore dataStore, SimpleFeatureCollection featureCollection) throws IOException {
        Transaction transaction = new DefaultTransaction("create");
        String typeName = dataStore.getTypeNames()[0]; // shp文件名称?
        SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);

        // 写入要素
        featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(featureCollection);
            transaction.commit();
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
        }
    }


    private static void addFeature(SimpleFeature feature, FeatureWriter<SimpleFeatureType, SimpleFeature> writer) throws IOException {

        SimpleFeature toWrite = writer.next();
        for (int i = 0; i < toWrite.getType().getAttributeCount(); i++) {
            String name = toWrite.getType().getDescriptor(i).getLocalName();
            toWrite.setAttribute(name, feature.getAttribute(name));
        }

        // copy over the user data
        if (feature.getUserData().size() > 0) {
            toWrite.getUserData().putAll(feature.getUserData());
        }

        // perform the write
        writer.write();
    }

}

使用示例

当初写的测试类,并没有全用到

public class SHPUtilTest {

    private static SimpleFeatureType createFeatureType() throws FactoryException {

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        // set global state
        builder.setName("Location");
        // 设置坐标系的几种方式
//        builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system
//        builder.setCRS(CRS.decode("EPSG:4326")); // <- Coordinate reference system
        builder.setSRS("EPSG:4326");

        // add attributes in order
        builder.add("the_geom", Point.class);
        builder.length(15).add("Name", String.class); // <- 15 chars width for name field
        builder.length(15).add("number", Integer.class);

        // build the type
        return builder.buildFeatureType();
    }

    public static void write1() {
        try {
            // 定义要素类型
            final SimpleFeatureType TYPE = createFeatureType();
            DefaultFeatureCollection collection = new DefaultFeatureCollection("internal", TYPE);
            GeometryFactory geometryFactory = new GeometryFactory();
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

            // 设置属性
            double latitude = Double.parseDouble("117.123456789");
            double longitude = Double.parseDouble("35.320001");
            String Name = "2050003092";
            String Number = "0";
            // 使用geometryFactory 创建要素
            Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
            Object[] obj = {point, Name, Number};
            // 使用SimpleFeatureBuilder创建要素
            SimpleFeature feature = featureBuilder.buildFeature(null, obj);
            collection.add(feature);

            ShapeFileWriterUtils.appendFeature(collection, "D:\\newPoi.shp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copy1(String inPath, String outPath) throws Exception {
        File inFile = new File(inPath);
        if (!inFile.exists()) {
            throw new Exception("inFile not exist");
        }
        File outFile = new File(outPath);

// Read
        DataStore inputDataStore = DataStoreFinder.getDataStore(
                Collections.singletonMap("url", inFile.toURI().toURL()));

        String inputTypeName = inputDataStore.getTypeNames()[0];
        SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

        FeatureSource<SimpleFeatureType, SimpleFeature>
                source = inputDataStore.getFeatureSource(inputTypeName);

        FeatureCollection<SimpleFeatureType, SimpleFeature>
                inputFeatureCollection = source.getFeatures();

// Write
        ShapefileDataStoreFactory dataStoreFactory =
                new ShapefileDataStoreFactory();
        ShapefileDataStore newDataStore =
                (ShapefileDataStore) dataStoreFactory.createNewDataStore(
                        Collections.singletonMap("url", outFile.toURI().toURL()));

        newDataStore.createSchema(inputType);
        String typeName = newDataStore.getTypeNames()[0];

        SimpleFeatureStore featureStore =
                (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

        featureStore.addFeatures(inputFeatureCollection);
    }

    public static void queryFeatures() throws Exception {
        Filter filter = CQL.toFilter("Name = '2050003092'");
        ShapeFileWriterUtils.removeFeatures(filter, "D:\\newPoi.shp");
    }

    public static void modifyTest() throws Exception {
        String path = "D:\\test.shp";
        Filter filter = CQL.toFilter("river_name = ''");
        String[] attrs = new String[]{"river_name", "county_id", "city_id"};
        Object[] values = new Object[]{"无名河", "320503", "320500"};

        ShapeFileWriterUtils.modifyFeatures(attrs, values, filter, path);
    }

    public static void main(String[] args) throws Exception {
        modifyTest();
    }
}
org.geotools org.geotools.arcsde org.geotools.arcsde.data org.geotools.arcsde.data.versioning org.geotools.arcsde.data.view org.geotools.arcsde.filter org.geotools.arcsde.gce org.geotools.arcsde.gce.band org.geotools.arcsde.gce.imageio org.geotools.arcs de.gce.producer org.geotools.arcsde.pool org.geotools.axis org.geotools.brewer.color org.geotools.coverage org.geotools.coverage.grid org.geotools.coverage.grid.io org.geotools.coverage.grid.io.imageio org.geotools.coverage.io org.geotools.coverage.processing org.geotools.coverage.processing.operation org.geotools.data org.geotools.data.collection org.geotools.data.crs org.geotools.data.db2 org.geotools.data.db2.filter org.geotools.data.dir org.geotools.data.gml org.geotools.data.gpx org.geotools.data.gpx.temporal org.geotools.data.h2 org.geotools.data.jdbc org.geotools.data.jdbc.attributeio org.geotools.data.jdbc.datasource org.geotools.data.jdbc.fidmapper org.geotools.data.jdbc.referencing org.geotools.data.memory org.geotools.data.mif org.geotools.data.mysql org.geotools.data.oracle org.geotools.data.oracle.attributeio org.geotools.data.oracle.referencing org.geotools.data.oracle.sdo org.geotools.data.ows org.geotools.data.postgis org.geotools.data.postgis.attributeio org.geotools.data.postgis.collection org.geotools.data.postgis.fidmapper org.geotools.data.postgis.referencing org.geotools.data.property org.geotools.data.shapefile org.geotools.data.shapefile.dbf org.geotools.data.shapefile.indexed org.geotools.data.shapefile.indexed.attribute org.geotools.data.shapefile.prj org.geotools.data.shapefile.shp org.geotools.data.shapefile.shp.xml org.geotools.data.store org.geotools.data.tiger org.geotools.data.view org.geotools.data.vpf org.geotools.data.vpf.exc org.geotools.data.vpf.file org.geotools.data.vpf.ifc org.geotools.data.vpf.io org.geotools.data.vpf.readers org.geotools.data.vpf.util org.geotools.data.wfs org.geotools.data.wms org.geotools.data.wms.request org.geotools.data.wms.response org.geotools.data.wms.xml org.geotools.demo org.geotools.demo.data org.geotools.demo.example org.geotools.demo.features org.geotools.demo.geometry org.geotools.demo.introduction org.geotools.demo.jts org.geotools.demo.libraryJTS org.geotools.demo.main org.geotools.demo.mappane org.geotools.demo.metadata.example org.geotools.demo.postgis org.geotools.demo.swing org.geotools.demo.swing.process org.geotools.demo.widgets org.geotools.demo.xml org.geotools.display.canvas org.geotools.display.canvas.map org.geotools.display.event org.geotools.display.geom org.geotools.display.style org.geotools.factory org.geotools.feature org.geotools.feature.collection org.geotools.feature.simple org.geotools.feature.type org.geotools.feature.visitor org.geotools.filter org.geotools.filter.capability org.geotools.filter.expression org.geotools.filter.function org.geotools.filter.function.math org.geotools.filter.identity org.geotools.filter.parser org.geotools.filter.spatial org.geotools.filter.text.cql2 org.geotools.filter.text.txt org.geotools.filter.v1_0 org.geotools.filter.v1_0.capabilities org.geotools.filter.v1_1 org.geotools.filter.v1_1.capabilities org.geotools.filter.visitor org.geotools.gce.arcgrid org.geotools.gce.geotiff org.geotools.gce.geotiff.crs_adapters org.geotools.gce.geotiff.IIOMetadataAdpaters org.geotools.gce.geotiff.IIOMetadataAdpaters.utils org.geotools.gce.geotiff.IIOMetadataAdpaters.utils.codes org.geotools.gce.gtopo30 org.geotools.gce.image org.geotools.gce.imagemosaic org.geotools.gce.imagepyramid org.geotools.geometry org.geotools.geometry.array org.geotools.geometry.coordinatesequence org.geotools.geometry.iso org.geotools.geometry.iso.aggregate org.geotools.geometry.iso.complex org.geotools.geometry.iso.coordinate org.geotools.geometry.iso.index org.geotools.geometry.iso.index.quadtree org.geotools.geometry.iso.io org.geotools.geometry.iso.io.wkt org.geotools.geometry.iso.operation org.geotools.geometry.iso.operation.overlay org.geotools.geometry.iso.operation.relate org.geotools.geometry.iso.primitive org.geotools.geometry.iso.root org.geotools.geometry.iso.topograph2D org.geotools.geometry.iso.topograph2D.index org.geotools.geometry.iso.topograph2D.util org.geotools.geometry.iso.util org.geotools.geometry.iso.util.algorithm2D org.geotools.geometry.iso.util.algorithmND org.geotools.geometry.iso.util.elem2D org.geotools.geometry.iso.util.interpolation org.geotools.geometry.iso.util.topology org.geotools.geometry.jts org.geotools.geometry.jts.coordinatesequence org.geotools.geometry.jts.spatialschema org.geotools.geometry.jts.spatialschema.geometry org.geotools.geometry.jts.spatialschema.geometry.aggregate org.geotools.geometry.jts.spatialschema.geometry.complex org.geotools.geometry.jts.spatialschema.geometry.geometry org.geotools.geometry.jts.spatialschema.geometry.primitive org.geotools.geometry.text org.geotools.gml org.geotools.gml.producer org.geotools.gml2 org.geotools.gml2.bindings org.geotools.gml3 org.geotools.gml3.bindings org.geotools.gml3.bindings.smil org.geotools.gml3.smil org.geotools.gpx org.geotools.gpx.bean org.geotools.gpx.binding org.geotools.graph.build org.geotools.graph.build.basic org.geotools.graph.build.feature org.geotools.graph.build.line org.geotools.graph.build.opt org.geotools.graph.build.polygon org.geotools.graph.io org.geotools.graph.io.standard org.geotools.graph.path org.geotools.graph.structure org.geotools.graph.structure.basic org.geotools.graph.structure.line org.geotools.graph.structure.opt org.geotools.graph.traverse org.geotools.graph.traverse.basic org.geotools.graph.traverse.standard org.geotools.graph.util org.geotools.graph.util.delaunay org.geotools.graph.util.geom org.geotools.graph.util.graph org.geotools.gui.headless org.geotools.gui.swing org.geotools.gui.swing.contexttree org.geotools.gui.swing.contexttree.column org.geotools.gui.swing.contexttree.node org.geotools.gui.swing.contexttree.popup org.geotools.gui.swing.contexttree.renderer org.geotools.gui.swing.crschooser org.geotools.gui.swing.datachooser org.geotools.gui.swing.datachooser.model org.geotools.gui.swing.demo org.geotools.gui.swing.event org.geotools.gui.swing.filter org.geotools.gui.swing.icon org.geotools.gui.swing.image org.geotools.gui.swing.map.map2d org.geotools.gui.swing.map.map2d.control org.geotools.gui.swing.map.map2d.decoration org.geotools.gui.swing.map.map2d.event org.geotools.gui.swing.map.map2d.handler org.geotools.gui.swing.map.map2d.listener org.geotools.gui.swing.map.map2d.strategy org.geotools.gui.swing.misc org.geotools.gui.swing.misc.filter org.geotools.gui.swing.misc.Render org.geotools.gui.swing.process org.geotools.gui.swing.propertyedit org.geotools.gui.swing.propertyedit.filterproperty org.geotools.gui.swing.propertyedit.model org.geotools.gui.swing.propertyedit.styleproperty org.geotools.gui.swing.referencing org.geotools.gui.swing.style org.geotools.gui.swing.style.sld org.geotools.gui.swing.table org.geotools.gui.swing.tree org.geotools.image org.geotools.image.io org.geotools.image.io.metadata org.geotools.image.io.mosaic org.geotools.image.io.netcdf org.geotools.image.io.stream org.geotools.image.io.text org.geotools.image.jai org.geotools.image.palette org.geotools.index org.geotools.index.quadtree org.geotools.index.quadtree.fs org.geotools.index.rtree org.geotools.index.rtree.cachefs org.geotools.index.rtree.database org.geotools.index.rtree.database.mysql org.geotools.index.rtree.fs org.geotools.index.rtree.memory org.geotools.io org.geotools.jdbc org.geotools.kml org.geotools.kml.bindings org.geotools.legend org.geotools.map org.geotools.map.event org.geotools.math org.geotools.measure org.geotools.metadata org.geotools.metadata.iso org.geotools.metadata.iso.citation org.geotools.metadata.iso.constraint org.geotools.metadata.iso.content org.geotools.metadata.iso.distribution org.geotools.metadata.iso.extent org.geotools.metadata.iso.identification org.geotools.metadata.iso.lineage org.geotools.metadata.iso.maintenance org.geotools.metadata.iso.quality org.geotools.metadata.iso.spatial org.geotools.metadata.sql org.geotools.nature org.geotools.openoffice org.geotools.ows org.geotools.ows.bindings org.geotools.ows.v1_1 org.geotools.parameter org.geotools.process org.geotools.process.impl org.geotools.process.literal org.geotools.referencing org.geotools.referencing.crs org.geotools.referencing.cs org.geotools.referencing.datum org.geotools.referencing.example org.geotools.referencing.factory org.geotools.referencing.factory.epsg org.geotools.referencing.factory.wms org.geotools.referencing.operation org.geotools.referencing.operation.builder org.geotools.referencing.operation.matrix org.geotools.referencing.operation.projection org.geotools.referencing.operation.transform org.geotools.referencing.piecewise org.geotools.referencing.wkt org.geotools.renderer org.geotools.renderer.i18n org.geotools.renderer.lite org.geotools.renderer.lite.gridcoverage2d org.geotools.renderer.shape org.geotools.renderer.shape.shapehandler.jts org.geotools.renderer.shape.shapehandler.simple org.geotools.renderer.style org.geotools.repository org.geotools.repository.adaptable org.geotools.repository.defaults org.geotools.repository.postgis org.geotools.repository.property org.geotools.repository.shapefile org.geotools.repository.styling org.geotools.repository.wfs org.geotools.repository.wms org.geotools.sld org.geotools.sld.bindings org.geotools.styling org.geotools.styling.visitor org.geotools.svg org.geotools.test org.geotools.text org.geotools.text.filter org.geotools.util org.geotools.util.logging org.geotools.utils org.geotools.utils.coveragetiler org.geotools.utils.imagemosaic org.geotools.utils.imageoverviews org.geotools.utils.imagepyramid org.geotools.utils.progress org.geotools.validation org.geotools.validation.attributes org.geotools.validation.dto org.geotools.validation.network org.geotools.validation.relate org.geotools.validation.spatial org.geotools.validation.xml org.geotools.wfs org.geotools.wfs.bindings org.geotools.wfs.protocol org.geotools.wfs.v_1_0_0.data org.geotools.wfs.v_1_1_0.data org.geotools.xlink org.geotools.xml org.geotools.xml.filter org.geotools.xml.gml org.geotools.xml.handlers org.geotools.xml.handlers.xsi org.geotools.xml.impl org.geotools.xml.impl.jxpath org.geotools.xml.schema org.geotools.xml.schema.impl org.geotools.xml.styling org.geotools.xml.test org.geotools.xml.transform org.geotools.xml.wfs org.geotools.xml.xLink org.geotools.xml.xsi org.geotools.xs org.geotools.xs.bindings org.geotools.xs.facets
可以使用Geotools库来读写shp文件。下面是一个简单的示例代码,可以读取shp文件并打印出其属性表信息: ```java import java.io.File; import java.io.IOException; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; public class ShpFileReader { public static void main(String[] args) throws IOException { // 读取shp文件 File file = new File("path/to/shapefile.shp"); DataStore dataStore = DataStoreFinder.getDataStore(file); String typeName = dataStore.getTypeNames()[0]; SimpleFeatureType schema = dataStore.getSchema(typeName); // 获取属性表信息 System.out.println("Feature Type: " + typeName); System.out.println("Number of attributes: " + schema.getAttributeCount()); System.out.println("Attributes: "); for (int i = 0; i < schema.getAttributeCount(); i++) { System.out.println(schema.getAttributeDescriptors().get(i).getName()); } // 获取要素信息 SimpleFeatureCollection collection = dataStore.getFeatureSource(typeName).getFeatures(); try (SimpleFeatureIterator features = collection.features()) { while (features.hasNext()) { SimpleFeature feature = features.next(); System.out.println(feature.getID() + ": " + feature.getDefaultGeometryProperty().getValue()); } } dataStore.dispose(); } } ``` 需要注意的是,需要在pom.xml中添加geotools依赖: ```xml <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>24.0</version> </dependency> ```
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值