Geotools基本增删改查Feature

yizhihongxing

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.cnblogs.com/walkAlwaysInCode/p/17347714.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Geotools基本增删改查Feature - Python技术站

(0)
上一篇 2023年4月24日 下午12:14
下一篇 2023年4月24日

相关文章

  • Java多线程之线程安全问题详情

    Java多线程之线程安全问题详情 什么是线程安全问题? 在多线程并发执行的过程中,若多个线程会同时访问同一个共享的数据,就有可能出现线程安全问题。 这种问题常见的形式就是多个线程操作同一份数据时,会产生竞态条件(Race Condition),导致数据的状态被破坏。 线程安全问题包括但不限于: 数据竞争(Data Race) 重入锁问题(Reentrant …

    Java 2023年5月18日
    00
  • java计算π的多种方法

    Java 计算 π 的多种方法 在计算机科学中,π(圆周率)是一个非常有趣且重要的数学常数。由于π是一个无理数,不能用有限长的小数表示,因此我们需要使用其他方法来近似计算π。本文将介绍几种 Java 中计算π的方法。 1. 蒙特卡罗方法 蒙特卡罗方法是一种基于随机抽样的数值计算方法。这个方法的基本思想是在一个正方形内随机放置若干个点,并且判断每个点是否在以正…

    Java 2023年5月19日
    00
  • 23种设计模式(8) java外观模式

    23种设计模式(8) Java外观模式 一、什么是外观模式? 外观模式(Facade Pattern)是一种结构型设计模式,它为子系统中的一组接口提供一个一致的接口,使得这个子系统更加容易被使用。 外观模式又称为门面模式,顾名思义,就是像房屋门面一样,将一个复杂的系统或一组类的接口封装起来,提供一个更加简单、易用的接口,使得外部用户通过这个接口就能够完成庞杂…

    Java 2023年5月24日
    00
  • Mybatis-Plus实现只更新部分字段的数据

    Mybatis-Plus是一个开源的Mybatis扩展工具库,提供了很多便捷的CRUD操作、代码自动生成等功能。在实现只更新部分字段的数据时,我们可以使用Mybatis-Plus提供的Wrapper类和UpdateWrapper类来实现。 具体步骤如下: 步骤一:引入Mybatis-Plus依赖 在项目的pom.xml文件中引入Mybatis-Plus的依赖…

    Java 2023年5月26日
    00
  • java限流算法详细

    Java限流算法详细攻略 什么是限流算法 限流算法是一种流行的控制流量的技术,通常是在高并发的系统中使用,用于控制请求的流量以避免系统过载。在某些情况下,如果系统不稳定地处理过多的请求,系统可能会崩溃,因此限流算法的作用显得尤为重要。 常见的限流算法 以下是几种常见的限流算法: 1.计数器算法 计数器算法是一种特别基础的算法,思路就是所有的请求都进入一个计数…

    Java 2023年5月19日
    00
  • maven加入spring框架的详细教程

    下面是关于“maven加入spring框架的详细教程”的完整攻略,包含两个示例说明。 maven加入spring框架的详细教程 Maven是一个流行的Java项目管理工具,它可以帮助我们更加方便地管理项目依赖关系。在Java项目中,Spring框架是一个非常流行的框架,它可以帮助我们更加方便地构建现代化的企业应用程序。本文将介绍如何使用Maven将Sprin…

    Java 2023年5月17日
    00
  • 微信小程序模板消息推送的两种实现方式

    微信小程序的模板消息可以让开发者向用户发送特定的消息,提醒用户进行相关操作等。模板消息的推送可以有两种实现方式,分别是通过微信后台的服务接口进行推送和通过开发者自行实现后端服务器进行推送。 通过微信后台的服务接口进行推送 该方式需要先进行微信公众号的设置并获取相关的服务接口信息,具体实现步骤如下: 登录微信公众平台,进入开发-开发者工具-接口测试页; 选择模…

    Java 2023年5月23日
    00
  • MyEclipse怎么设置性能才能达到最佳?

    MyEclipse是一款集成开发环境(IDE),使用该IDE能够为Java开发提供有效的工具和特性。MyEclipse有许多内置功能和选项,可以调整各种配置以提高性能和效率。以下是设置MyEclipse性能的攻略: 1. 显式地配置JVM MyEclipse是基于Java开发的IDE,其虚拟机运行在Java虚拟机上(JVM)。了解和调整JVM设置是提高性能的…

    Java 2023年5月20日
    00
合作推广
合作推广
分享本页
返回顶部