最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Geotools實(shí)現(xiàn)shape文件的寫入功能

 更新時間:2023年08月21日 10:21:37   作者:開放GIS  
Geotools作為開源的Java?GIS三方庫,已經(jīng)成為GIS服務(wù)器端的主流開源庫,其功能非常強(qiáng)大,涉及到GIS業(yè)務(wù)的方方面面,其中就包括GIS數(shù)據(jù)的讀寫,今天小編就借助Geotools來實(shí)現(xiàn)shape數(shù)據(jù)的寫入,需要的朋友可以參考下

裝配GeoTools有兩種方式,一種是配置maven工程的pom文件(配置方式參考官網(wǎng)),另一種是下載geotools的jar包到本地導(dǎo)入依賴。我采用的是下載jar的方式,下載路徑:https://sourceforge.net/projects/geotools/files/

眾所周知Geotools作為開源的Java GIS三方庫,已經(jīng)成為GIS服務(wù)器端的主流開源庫,其功能非常強(qiáng)大,涉及到GIS業(yè)務(wù)的方方面面,其中就包括GIS數(shù)據(jù)的讀寫,今天小編就借助Geotools來實(shí)現(xiàn)shape數(shù)據(jù)的寫入。

Geotools對于shape數(shù)據(jù)寫入,主要提供了SimpleFeatureStore和FeatureWriter兩個主要操作類,下面小編就根據(jù)這兩個類實(shí)現(xiàn)shape數(shù)據(jù)的寫入,廢話不多說,直接上代碼:

import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.opengis.feature.simple.SimpleFeature;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ShapwWriterTest {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\data\\line_sheng.shp");
        ShapefileDataStore shapefileDataStore = new ShapefileDataStore(file.toURI().toURL());
        SimpleFeatureSource simpleFeatureSource = shapefileDataStore.getFeatureSource();
        int count = simpleFeatureSource.getFeatures().size();
        for(int i = 0;i<2; i++){
            //分批插入(沒啥邏輯,主要是驗(yàn)證多次寫入同一個shp)
            Query query = createQuery(i*(count / 2),count / 2);
            SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures(query);
            addFeature2Shp(simpleFeatureCollection,"D:\\data\\line_sheng_1.shp");
        }
    }
    /**
     * 將simplefearurecollection寫入目標(biāo)shape
     * @param simpleFeatureCollection
     * @param filePath
     * @throws IOException
     */
    public static void addFeature2Shp(SimpleFeatureCollection simpleFeatureCollection, String filePath) throws IOException {
        File file = new File(filePath);
        ShapefileDataStore shapefileDataStore = null;
        if (file.exists()){
            shapefileDataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(Collections.singletonMap("url",file.toURI().toURL()));
        }else{
            ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
            shapefileDataStore = (ShapefileDataStore) shapefileDataStoreFactory.createNewDataStore(Collections.singletonMap("url",file.toURI().toURL()));
            shapefileDataStore.setCharset(Charset.defaultCharset());
            shapefileDataStore.createSchema(simpleFeatureCollection.getSchema());
        }
        //獲取simplefeaturestore
        writerFeature(simpleFeatureCollection, shapefileDataStore);
        //writerFeature1(simpleFeatureCollection,shapefileDataStore);
    }
    /**
     * 使用SimpleFeatureStore寫入shape文件
     * @param simpleFeatureCollection
     * @param shapefileDataStore
     * @throws IOException
     */
    private static void writerFeature(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
        SimpleFeatureStore simpleFeatureStore = (SimpleFeatureStore) shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
        Transaction transaction = new DefaultTransaction("create");
        simpleFeatureStore.setTransaction(transaction);
        try {
            simpleFeatureStore.addFeatures(simpleFeatureCollection);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            transaction.close();
        }
    }
    /**
     * 使用FeatureWriter來寫feature
     * @param simpleFeatureCollection
     * @param shapefileDataStore
     * @throws IOException
     */
    private static void writerFeature1(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
        FeatureWriter featureWriter = shapefileDataStore.getFeatureWriterAppend(Transaction.AUTO_COMMIT);
        SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
        while(simpleFeatureIterator.hasNext()){
            SimpleFeature simpleFeature = simpleFeatureIterator.next();
            SimpleFeature simpleFeature1 = (SimpleFeature) featureWriter.next();
            simpleFeature1.setAttributes(simpleFeature.getAttributes());
        }
        featureWriter.write();
        featureWriter.close();
        simpleFeatureIterator.close();
    }
    private static Query createQuery(int startIndex,int queryCount){
        Query query = new Query();
        query.setStartIndex(startIndex);
        query.setMaxFeatures(queryCount);
        return query;
    }
    /**
     * 總結(jié)geotools 讀取shape的幾種方式
     */
    private static void testReaderShape(String filePath) throws IOException {
        //第一種方式
        ShapefileDataStore shapefileDataStore = new ShapefileDataStore(new File(filePath).toURI().toURL());
        /**
         * 使用上述這種方式讀shape的話,其中的很多參數(shù)都是默認(rèn)的,最主要的是它的編碼是StandardCharsets.ISO_8859_1
         * 因此我們需要單獨(dú)設(shè)置下
         */
        shapefileDataStore.setCharset(Charset.forName("UTF-8"));
        //第二種ShapefileDataStoreFactory
        ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
        Map<String,?> paramMap = new HashMap<>();
        /**
         * 通常有那些參數(shù),我們可以通過下面的這個函數(shù)去查看,這里面
         */
        shapefileDataStoreFactory.createNewDataStore(paramMap);
        //第三種方式,這種方式可適用于各種基于SPI模式的文件讀寫
        DataStoreFinder.getDataStore(paramMap);
    }
}

好了,今天關(guān)于Geotools寫入shape的代碼就分享到這里,而關(guān)于shape文件的操作,還有很多內(nèi)容,其中最主要的過濾(Filter)后續(xù)也會出個專題來記錄下,畢竟這里的東西很多。

相關(guān)文章

  • springboot2.6.7集成springfox3.0.0的示例代碼

    springboot2.6.7集成springfox3.0.0的示例代碼

    這篇文章主要介紹了springboot2.6.7集成springfox3.0.0的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Spring Boot之內(nèi)嵌tomcat版本升級操作示例

    Spring Boot之內(nèi)嵌tomcat版本升級操作示例

    這篇文章主要為大家介紹了Spring Boot之內(nèi)嵌tomcat版本升級操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • spring?boot?executable?jar/war?原理解析

    spring?boot?executable?jar/war?原理解析

    spring boot里其實(shí)不僅可以直接以 java -jar demo.jar的方式啟動,還可以把jar/war變?yōu)橐粋€可以執(zhí)行的腳本來啟動,比如./demo.jar,這篇文章主要介紹了spring?boot?executable?jar/war?原理,需要的朋友可以參考下
    2023-02-02
  • 使用Spring開啟@Async異步方式(javaconfig配置)

    使用Spring開啟@Async異步方式(javaconfig配置)

    這篇文章主要介紹了使用Spring開啟@Async異步方式(javaconfig配置),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解

    Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解

    這篇文章主要介紹了Spring框架IOC兩種創(chuàng)建工廠方法詳解,文中附含詳細(xì)的代碼示例分別對靜態(tài)方法和實(shí)例方法創(chuàng)建工廠作了簡要的分析
    2021-09-09
  • 利用過濾器修改response中的返回值

    利用過濾器修改response中的返回值

    文章介紹了如何通過繼承HttpServletResponseWrapper并重寫Response對象的方法來獲取response中的返回值,同時還分享了如何配置過濾器
    2024-12-12
  • Spring源碼解密之自定義標(biāo)簽與解析

    Spring源碼解密之自定義標(biāo)簽與解析

    這篇文章主要給大家介紹了關(guān)于Spring源碼解密之自定義標(biāo)簽與解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • SpringBoot學(xué)習(xí)之基于注解的緩存

    SpringBoot學(xué)習(xí)之基于注解的緩存

    spring boot對緩存支持非常靈活,我們可以使用默認(rèn)的EhCache,也可以整合第三方的框架,只需配置即可,下面這篇文章主要給大家介紹了關(guān)于SpringBoot學(xué)習(xí)之基于注解緩存的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • 淺析Java 并發(fā)編程中的synchronized

    淺析Java 并發(fā)編程中的synchronized

    這篇文章主要介紹了Java 并發(fā)編程中的synchronized的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java并發(fā)編程,感興趣的朋友可以了解下
    2020-12-12
  • SpringBoot + MySQL 實(shí)現(xiàn) SSL 連接配置

    SpringBoot + MySQL 實(shí)現(xiàn) SSL 連接配置

    本文主要介紹了SpringBoot + MySQL 實(shí)現(xiàn) SSL 連接配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-05-05

最新評論

忻州市| 泸水县| 江孜县| 平江县| 瓦房店市| 白水县| 吴江市| 九台市| 梅河口市| 九龙县| 瑞丽市| 丰顺县| 云林县| 云南省| 监利县| 兴隆县| 久治县| 长顺县| 吴川市| 双峰县| 佛冈县| 仪陇县| 慈溪市| 当雄县| 宣城市| 工布江达县| 清丰县| 福建省| 华阴市| 安达市| 汶川县| 航空| 兰坪| 屏山县| 江城| 丰城市| 万源市| 历史| 巴塘县| 白银市| 常山县|