【GeoTools】geotools-20 讀取、寫入shape文件

shape文件至少包含shp、dbf、shx文件,否則會報錯。shp文件存儲地理空間信息;dbf文件存儲屬性信息;shx文件存儲索引信息。

注意:關於import org.locationtech.jts.geom.Geometry和import com.vividsolutions.jts.geom.Geometry前者是新版本所採用的JTS,後者是老版本採用的JTS,你要採用像geotools的20版本,用前者;用geotool的18版本以前的用後者。否則會報錯。
 

1,讀取shape文件:

public static void readShapefile() throws IOException{
        String shapefile = "E:\\supermap_data\\lalala\\my.shp";
        File file = new File(shapefile);
        // 申明一個存儲空間
        ShapefileDataStore shapDataStore = null;
        // 將文件讀取到工作空間
        shapDataStore = new ShapefileDataStore(file.toURL());
        // 設置編碼,防止中文亂碼
        Charset charset = Charset.forName("GBK");
        shapDataStore.setCharset(charset);
        // 獲取圖層名稱
        String typeName = shapDataStore.getTypeNames()[0];
        System.out.println(typeName);
        SimpleFeatureSource featureSource = null;
        // 根據圖層名稱來獲取要素的source
        featureSource = shapDataStore.getFeatureSource(typeName);
        // 獲取要素集
        SimpleFeatureCollection result = featureSource.getFeatures();
        // 獲取要素集合,方便進行迭代讀取每一個要素
        SimpleFeatureIterator iterator = result.features();
        while (iterator.hasNext()){
            // 獲取每一個要素
            SimpleFeature feature = iterator.next();
            System.out.println(feature.getID() + " " + feature.getDefaultGeometry());
        }
    }

2,寫入shape文件

 public static void writeShapefile() throws SchemaException, IOException {
        String path="E:\\supermap_data\\lalala\\my.shp";
        //1.創建shape文件對象
        File file =new File(path);
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        //用於捕獲參數需求的數據類
        //URLP:url to the .shp file.
        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
        //2.創建一個新的數據存儲——對於一個還不存在的文件。
        ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
        //3.定義圖形信息和屬性信息
        //SimpleFeatureTypeBuilder 構造簡單特性類型的構造器
        SimpleFeatureTypeBuilder tBuilder = new SimpleFeatureTypeBuilder();
        //設置
        //WGS84:一個二維地理座標參考系統,使用WGS84數據
//        tBuilder.setCRS(DefaultGeographicCRS.WGS84);
        // 需要添加 gt-epsg-hsql 架包
        tBuilder.setSRS( "EPSG:4490" );

        tBuilder.setName("shapefile");
        //添加 一個點
        tBuilder.add("the_geom", Point.class);
        //添加一個id
        tBuilder.add("osm_id", Long.class);
        //添加名稱
        tBuilder.add("name", String.class);
        //添加描述
        tBuilder.add("des", String.class);
        //設置此數據存儲的特徵類型
        ds.createSchema(tBuilder.buildFeatureType());
        //設置編碼
        ds.setCharset(Charset.forName("UTF-8"));
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
        SimpleFeature feature = writer.next();
        double x = 116.123; //X軸座標
        double y = 39.345 ; //Y軸座標
        Coordinate coordinate = new Coordinate(x, y);
        Point point = new GeometryFactory().createPoint(coordinate);
        feature.setAttribute("the_geom",point);
        feature.setAttribute("osm_id", 1234567890l);
        feature.setAttribute("name", "帥哥");
        feature.setAttribute("des", "愛美女");

        feature = writer.next();
        x = 116.456;
        y = 39.678 ;
        coordinate = new Coordinate(x, y);
        point = new GeometryFactory().createPoint(coordinate);
        feature.setAttribute("the_geom",point);
        feature.setAttribute("osm_id", 1234567891l);
        feature.setAttribute("name", "美女");
        feature.setAttribute("des", "愛帥哥");
        writer.write();
        writer.close();
        ds.dispose();
    }

 

3,使用的是geotools-20.2,maven配置文件如下:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>20.2</geotools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</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-main</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools.jdbc</groupId>
            <artifactId>gt-jdbc-postgis</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>

    </dependencies>

    <repositories>
        <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>opengeo</id>
            <name>OpenGeo Maven Repository</name>
            <url>http://repo.opengeo.org</url>
        </repository>
    </repositories>

參考博客:

1,Java+GeoTools工具包+讀寫shapfile文件

https://www.giserdqy.com/secdev/geotools/27801/

 

好的博文:

1,PostGIS的安裝與初步使用

https://blog.csdn.net/qq_35732147/article/details/81169961

2,Windows環境下GeoTools 連接PostGIS時候一些坑

https://blog.csdn.net/chajizhuo3103/article/details/80353056

3,geotools中的空間關係(Geometry Relationships)和空間操作(Geometry Operations)
https://blog.csdn.net/gisshixisheng/article/details/56302250

4,geotools實現兩個shp的相交計算
https://www.jianshu.com/p/08c78e00b7a8

5,postgis與geotools對應方法總結
https://blog.csdn.net/lnxyangruosong/article/details/80888808

6,GeoTools應用-JTS(Geometry之間的關係)
https://blog.csdn.net/sxausgyy/article/details/8113077

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章