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

Geotools基本增刪改查Feature

2023-04-24 07:45:28 後端開發

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/550927.html

標籤:其他

上一篇:從0開始學習c++

下一篇:返回列表

標籤雲
其他(157910) Python(38094) 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:45:28 more
  • 從0開始學習c++

    常量指標與指標常量 #include<iostream> using namespace std; int main() { int a = 10; int b = 20; // 常量指標與指標常量 // 1.常量指標 const修飾指標 指標的指向是可以修改的(指標變數中存的地址值可以修改) 指標 ......

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

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

    uj5u.com 2023-04-24 07:45:20 more
  • 最短路徑問題

    平面上有n個點(n<=100),每個點的坐標均在-10000~10000之間,其中的一些點之間有連線。 若有連線,則表示可從一個點到達另一個點,即兩點間有通路,同路的距離為兩點間的直線距離。現在的任務是找出從一點到另一點之間的最短路徑。 小提示: 兩點的距離:如果點$A$坐標為$(x_A,y_A)$ ......

    uj5u.com 2023-04-24 07:45:15 more
  • Django筆記二十九之中間件介紹

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十九之中間件介紹 這一節介紹一下 Django 的中間件。 關于中間件,官方檔案的解釋為:中間件是一個嵌入 Django 系統的 request 和 response 的鉤子框架,是一個能夠全域改變 Django 輸入/輸出的系統。 我 ......

    uj5u.com 2023-04-24 07:45:08 more
  • 15面向物件特性

    面向物件特性 封裝 在程式設計中,封裝(Encapsulation)是對具體物件的一種抽象,即將某些部分隱藏起來,在程式外部看不到,其含義是其他程式無法呼叫。要了解封裝,離不開“私有化”,就是將類或者是函式中的某些屬性限制在某個區域之內,外部無法呼叫。 封裝的作用: 1、保護隱私(把不想別人知道的東 ......

    uj5u.com 2023-04-24 07:45:03 more
  • 【Qt6】QWindow類可以做什么

    原來的水文標題是“用 VS Code 搞 Qt6”,想想還是直接改為“Qt6”,反正這個用不用 VS Code 也能搞。雖然我知道大伙伴們都很討厭 CMake,但畢竟這廝幾乎成了 C++ 的玩家規范了。Qt 也算識大體,支持用 CMake 來構建程式。所以,只要你用的是能寫 C++ 的工具,理論上都 ......

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

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

    uj5u.com 2023-04-24 07:44:44 more
  • springboot~關于md5簽名引發的問題

    事實是這樣的,我有個介面,這個介面不能被篡改,于是想到了比較簡單的md5對url地址引數進行加密,把這個密碼當成是sign,然后服務端收到請求后,使用相同演算法也生成sign,兩個sign相同就正常沒有被篡改過。 問題的出現 介面中的引數包括userId,extUserId,時間,其中extUserI ......

    uj5u.com 2023-04-24 07:44:38 more
  • Java中代碼的執行順序

    結論 注意 只有顯式的加載類 JVM才會加載到記憶體中 先加載父類的靜態代碼塊 然后執行子類靜態代碼塊 當前類存在類靜態變數注意參考型別沒進行賦值操作初始化為null 并不會顯式的加載類又存在靜態代碼塊 會先執行前者進行初始化 再執行靜態代碼塊 在實體化類的時候 執行順序 構造代碼塊-->構造方法存在 ......

    uj5u.com 2023-04-24 07:44:33 more