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++
下一篇:返回列表
