Geotools實(shí)現(xiàn)shape文件的寫入功能
裝配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的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-04-04
Spring Boot之內(nèi)嵌tomcat版本升級操作示例
這篇文章主要為大家介紹了Spring Boot之內(nèi)嵌tomcat版本升級操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
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配置),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解
這篇文章主要介紹了Spring框架IOC兩種創(chuàng)建工廠方法詳解,文中附含詳細(xì)的代碼示例分別對靜態(tài)方法和實(shí)例方法創(chuàng)建工廠作了簡要的分析2021-09-09
SpringBoot + MySQL 實(shí)現(xiàn) SSL 連接配置
本文主要介紹了SpringBoot + MySQL 實(shí)現(xiàn) SSL 連接配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-05-05

