主頁 > 後端開發 > Geotools基本增刪改查Feature

Geotools基本增刪改查Feature

2023-04-24 07:36:52 後端開發

postgis依賴


<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>27.2</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-jdbc-postgis</artifactId>
    <version>27.2</version>
</dependency>

創建連接JDBCDataStore


Map<String, String> params = Map.of(
    PostgisNGDataStoreFactory.HOST.key, host,
    PostgisNGDataStoreFactory.PORT.key, port,
    PostgisNGDataStoreFactory.DATABASE.key, database,
    PostgisNGDataStoreFactory.SCHEMA.key, schema,
    PostgisNGDataStoreFactory.USER.key, user,
    PostgisNGDataStoreFactory.PASSWD.key, passwd,
    PostgisNGDataStoreFactory.DBTYPE.key, dbtype
);
JDBCDataStore jdbcDataStore = (JDBCDataStore)DataStoreFinder.getDataStore(params);

JDBCDataStore連接引數

Parameter Description
dbtype Must be the string postgis
host Machine name or IP address to connect to
port Port number to connect to, default 5432
schema The database schema to access
database The database to connect to
user User name
passwd Password
loose bbox Flag controlling loose bbox comparisons, default is true
preparedStatements Flag controlling whether prepared statements are used, default is false
encode functions Flag controlling if some common functions can be encoded into their SQL equivalent

連接池引數

Parameter Description
max connections Maximum number of connection the pool will hold at any time, default is 10
min connections Minimum number of connection the pool will hold at any time, default is 1
connection timeout Maximum number of second the pool will wait when trying to obtain a connection, default is 20 seconds
validate connections Flag controlling if the pool should validate connections when a new connection is obtained
Max open prepared statements Maximum number of prepared statements kept open and cached for each connection in the pool. Set to 0 to have unbounded caching, -1 to disable
Test while idle Periodically test if the connections are still valid also while idle in the pool
Time between evictor runs Number of seconds between idle object evictor runs. The default value is 300 seconds.
Min evictable time Number of seconds a connection needs to stay idle before the evictor starts to consider closing it
Evictor tests per run Number of connections checked by the idle connection evictor for each of its runs. The default value is 3 connections.

過濾器-Filter

使用過濾器來定義要對其進行操作的Feature集合,過濾器也可以組合成一個操作集合使用,
簡單說,過濾器相當于SQL陳述句的WHERE子句中存在的資訊,
Filter有多個子類,實作了許多型別的過濾器,包括簡單的屬性比較和空間查詢,

// 普通欄位
FilterFactory ff = CommonFactoryFinder.getFilterFactory();

/**
 * field 欄位名
 * value 條件值
 * geometry 條件幾何體
 * 
 * 
 * matchCase 是否區分大小寫,默認true-區分
 * MatchAction(實作MultiValuedFilter的會有),匹配邏輯
 * MatchAction.ANY-任何一個滿足,默認值
 * MatchAction.ALL-全部滿足
 * MatchAction.ONE-只有一個滿足
 * */

PropertyIsEqualTo equal = ff.equal(ff.property(field), ff.literal(value), true);//等于
PropertyIsLike like = ff.like(ff.property(field), "%keywords%");//模糊匹配
PropertyIsNotEqualTo notEqualTo = ff.notEqual(ff.property(field), ff.literal(value));//不等于
PropertyIsNull aNull = ff.isNull(ff.property(field));//null
PropertyIsGreaterThan greater = ff.greater(ff.property(field), ff.literal(value));// 大于
PropertyIsGreaterThanOrEqualTo greaterOrEqual = ff.greaterOrEqual(ff.property(field), ff.literal(value));// 大于等于
PropertyIsLessThan less = ff.less(ff.property(field), ff.literal(value));//小于
PropertyIsLessThanOrEqualTo lessOrEqual = ff.lessOrEqual(ff.property(field), ff.literal(value));//小于等于
PropertyIsBetween between = ff.between(ff.property(field), ff.literal(value), ff.literal(value));//在...之間
During during = ff.during(ff.property(field), ff.literal(value));//在時間期間
Before before = ff.before(ff.property(field), ff.literal(value));//在時間之前
After after = ff.after(ff.property(field), ff.literal(value));//在時間之后


// Geometry欄位
FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2();

Beyond beyond = ff2.beyond(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry), 100.0, Units.METRE.name);// 圖層幾何欄位超出給定幾何100米距離的
Contains contains = ff2.contains(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 圖層幾何欄位包含給定幾何
Within within = ff2.within(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 圖層幾何欄位被給定幾何包含
Intersects intersects = ff2.intersects(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 圖層幾何欄位與給定幾何相交
Disjoint disjoint = ff2.disjoint(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 圖層幾何欄位與給定幾何不相交
Touches touches = ff2.touches(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 圖層幾何欄位與給定幾何相切


// filter集合的邏輯關系,and并,or或,not非
And and = ff.and(List.of(equal,like,beyond));//
Or or = ff.or(List.of(notEqualTo,greater,contains));
Not not = ff.not(during);

// Function的實作類具體實作函式,name-函式名,例如:min,strReplace,toWKT
Function function = ff.function(name,expr1,exprN);

PropertyName property = ff.property(field);
Literal v = ff.literal(value);
Function min = ff.function("min", property, v);


PropertyName property = ff.property(field);
Literal search = ff.literal("search");
Literal replace = ff.literal("replace");
Literal all = ff.literal( true );
Function replace = ff.function("strReplace", new Expression[]{property,search,replace,all});

PropertyName property = ff.property(featureSource.schema.geometryDescriptor.localName);
Function toWKT = ff.function("toWKT", property);

查詢


/**
 * tableName 表名
 * filter 過濾器
 * List<String> propNames 欄位名串列
 * 
 * startIndex 起始位
 * maxFeatures 最大條數
 * sortField 排序欄位名
 * */
ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);

//回傳欄位列
List<PropertyName> propertyNames = propNames.stream().map(ff::property).collect(Collectors.toList());
Query query = new Query(tableName,filter,propertyNames);
int count = featureSource.getCount(query);//計數
// 分頁,倒序
query.setStartIndex(startIndex);
query.setMaxFeatures(maxFeatures);
query.setSortBy(new SortByImpl(ff.property(sortField), SortOrder.DESCENDING));

ContentFeatureCollection collection = featureSource.getFeatures(query);
SimpleFeatureIterator iterator = collection.features();
// SimpleFeatureIterator必須關閉,否則會造成記憶體泄漏
iterator.close();

新增

/**
* tableName 圖層名
* fieldName1 欄位名
* fieldValue1 欄位值
**/

ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(store.getSchema());
featureBuilder.set(fieldName1,fieldValue1);
featureBuilder.set(fieldNameN,fieldValueN);
SimpleFeature feature = featureBuilder.buildFeature(null);
ListFeatureCollection featureCollection = new ListFeatureCollection(store.getSchema(), List.of(feature));
List<FeatureId> addFeatures = store.addFeatures(featureCollection);

洗掉

/**
*
* typeName 圖層名
* fliter 過濾條件
*
*/
ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
store.removeFeatures(filter);

修改

/**
* 
* typeName 圖層名
* names 修改欄位名陣列
* values 修改值陣列
* fliter 過濾條件
* names和values順序保持一致
*/
ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
store.modifyFeature(names, values, filter)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/550909.html

標籤:Java

上一篇:【深入淺出Spring原理及實戰】「原始碼除錯分析」深入原始碼探索Spring底層框架的的refresh方法所出現的問題和例外

下一篇:返回列表

標籤雲
其他(157894) Python(38092) JavaScript(25383) Java(17988) C(15215) 區塊鏈(8258) C#(7972) AI(7469) 爪哇(7425) MySQL(7137) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4558) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2430) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1959) Web開發(1951) HtmlCss(1921) python-3.x(1918) 弹簧靴(1913) C++(1910) xml(1889) PostgreSQL(1872) .NETCore(1854) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Geotools基本增刪改查Feature

    postgis依賴 <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>27.2</version> </dependency> <dependency> <groupId>or ......

    uj5u.com 2023-04-24 07:36:52 more
  • 【深入淺出Spring原理及實戰】「原始碼除錯分析」深入原始碼探索

    學習Spring原始碼的建議 閱讀Spring官方檔案,了解Spring框架的基本概念和使用方法。 下載Spring原始碼,可以從官網或者GitHub上獲取。 閱讀Spring原始碼的入口類,了解Spring框架的啟動程序和核心組件的加載順序。 閱讀Spring原始碼中的注釋和檔案,了解每個類和方法的作用和 ......

    uj5u.com 2023-04-24 07:36:47 more
  • docker常用命令

    #一、Docker基本概念 ###1.鏡像(Image) Docker 鏡像 是一個特殊的檔案系統,除了提供容器運行時所需的程式、庫、資源、配置等檔案外,還包含了一些為運行時準備的一些配置引數(如匿名卷、環境變數、用戶等)。鏡像 不包含 任何動態資料,其內容在構建之后也不會被改變。 docker的鏡 ......

    uj5u.com 2023-04-24 07:36:21 more
  • JTS空間坐標Geometry使用

    Geomtery子類圖 創建Geometry GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); // 點 Coordinate coord = new Coordinate(1, 1); Point po ......

    uj5u.com 2023-04-23 07:34:00 more
  • Rust中的宏:宣告宏和程序宏

    Rust中的宣告宏和程序宏 宏是Rust語言中的一個重要特性,它允許開發人員撰寫可重用的代碼,以便在編譯時擴展和生成新的代碼。宏可以幫助開發人員減少重復代碼,并提高代碼的可讀性和可維護性。Rust中有兩種型別的宏:宣告宏和程序宏。 宣告宏: 宣告宏是一種用于定義新的宏的語法。它使用macro_rul ......

    uj5u.com 2023-04-23 07:33:56 more
  • 影像金字塔

    影像金字塔 簡單來說就是 自下而上影像一步一步縮小 1 高斯金字塔(涉及高斯分布) 向下采樣(縮小,對金字塔來說是自下向上) 第一步: 高斯濾波去噪 第二部:將偶數行和列去掉 向上采樣(放大,對金字塔來說是自上向下) 第一步:在每個方向上擴大兩倍,新增的行和列填充0 第二步:利用之前同樣的內核進行卷 ......

    uj5u.com 2023-04-23 07:33:50 more
  • 第8章 多執行緒

    8.1 執行緒簡介 1 、多任務 現實生活中多件事一起作。 在程式中是指在一個系統中可以同時進行多個行程,即有多個單獨運行的任務,每一個任務對應一個行程。 每一個行程都有一段專用的記憶體區域,即使是多次啟動同一段程式產生不同的行程也是如此。 2、多執行緒 Java 給多執行緒編程提供了內置的支持。 一條執行緒 ......

    uj5u.com 2023-04-23 07:33:46 more
  • 影像邊緣檢測(Canny)

    Canny檢測的流程 Canny檢測主要是用于邊緣檢測 1)使用高斯濾波器,以平滑影像,濾除噪聲。 2)計算影像中每個像素點的梯度強度和方向。 3)應用非極大值(Non-Maximum Suppression)抑制,以消除邊緣檢測帶來的雜散回應 4)應用雙閾值(Double-Threshold)檢測 ......

    uj5u.com 2023-04-23 07:33:41 more
  • 洛谷:P5716日份天數

    題目描述 輸入年份和月份,輸出這一年的這一月有多少天。需要考慮閏年。 輸入格式 輸入兩個正整數,分別表示年份 $y$ 和月數 $m$,以空格隔開。 輸出格式 輸出一行一個正整數,表示這個月有多少天。 樣例 #1 樣例輸入 #1 1926 8 樣例輸出 #1 31 樣例輸入 #2 2000 2 樣例輸 ......

    uj5u.com 2023-04-23 07:33:33 more
  • 【Visual Leak Detector】原始碼檔案概覽

    說明 使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇對 VLD 原始碼包中的各檔案用途做個概述。同系列文章目錄可見 《記憶體泄漏檢測工具》目錄 1. 整體概覽 以 vld2.5.1 版本為例,下載原始碼 后,根目錄下一共 5 個檔案夾:.teamcity、lib、mfc_detect、set ......

    uj5u.com 2023-04-23 07:33:28 more