Geotools實(shí)現(xiàn)shape文件的寫入功能
裝配GeoTools有兩種方式,一種是配置maven工程的pom文件(配置方式參考官網(wǎng)),另一種是下載geotools的jar包到本地導(dǎo)入依賴。我采用的是下載jar的方式,下載路徑:https://sourceforge.net/projects/geotools/files/
眾所周知Geotools作為開源的Java GIS三方庫(kù),已經(jīng)成為GIS服務(wù)器端的主流開源庫(kù),其功能非常強(qiáng)大,涉及到GIS業(yè)務(wù)的方方面面,其中就包括GIS數(shù)據(jù)的讀寫,今天小編就借助Geotools來(lái)實(shí)現(xiàn)shape數(shù)據(jù)的寫入。
Geotools對(duì)于shape數(shù)據(jù)寫入,主要提供了SimpleFeatureStore和FeatureWriter兩個(gè)主要操作類,下面小編就根據(jù)這兩個(gè)類實(shí)現(xiàn)shape數(shù)據(jù)的寫入,廢話不多說(shuō),直接上代碼:
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++){ //分批插入(沒(méi)啥邏輯,主要是驗(yàn)證多次寫入同一個(gè)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來(lái)寫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ù),我們可以通過(guò)下面的這個(gè)函數(shù)去查看,這里面 */ shapefileDataStoreFactory.createNewDataStore(paramMap); //第三種方式,這種方式可適用于各種基于SPI模式的文件讀寫 DataStoreFinder.getDataStore(paramMap); } }
好了,今天關(guān)于Geotools寫入shape的代碼就分享到這里,而關(guān)于shape文件的操作,還有很多內(nèi)容,其中最主要的過(guò)濾(Filter)后續(xù)也會(huì)出個(gè)專題來(lái)記錄下,畢竟這里的東西很多。
相關(guān)文章
Java并發(fā)編程學(xué)習(xí)之ThreadLocal源碼詳析
這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程學(xué)習(xí)之源碼分析ThreadLocal的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06httpclient ConnectionHolder連接池連接保持源碼解析
這篇文章主要為大家介紹了httpclient ConnectionHolder連接池連接保持源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11SpringBoot集成thymeleaf渲染html模板的步驟詳解
這篇文章主要給大家詳細(xì)介紹了SpringBoot集成thymeleaf如何使實(shí)現(xiàn)html模板的渲染,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06Java實(shí)現(xiàn)批量操作Excel的示例詳解
在操作Excel的場(chǎng)景中,通常會(huì)有一些針對(duì)Excel的批量操作,以GcExcel為例,為大家詳細(xì)介紹一下Java是如何實(shí)現(xiàn)批量操作Excel的,需要的可以參考一下2023-07-07Java實(shí)現(xiàn)任務(wù)管理器性能網(wǎng)絡(luò)監(jiān)控?cái)?shù)據(jù)的方法詳解
在現(xiàn)代操作系統(tǒng)中,任務(wù)管理器是一個(gè)非常重要的工具,用于監(jiān)控和管理計(jì)算機(jī)的運(yùn)行狀態(tài),包括CPU使用率、內(nèi)存占用等,對(duì)于開發(fā)者和系統(tǒng)管理員來(lái)說(shuō),了解這些性能數(shù)據(jù)有助于優(yōu)化應(yīng)用程序和系統(tǒng)性能,本文將介紹如何使用Java編寫一個(gè)簡(jiǎn)單的程序來(lái)監(jiān)控網(wǎng)絡(luò)性能數(shù)據(jù)2025-01-01