jjzjj

Geotools基本增删改查Feature

行走于代码中 2023-11-13 原文

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)

有关Geotools基本增删改查Feature的更多相关文章

  1. Unity 热更新技术 | (三) Lua语言基本介绍及下载安装 - 2

    ?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------

  2. 计算机毕业设计ssm+vue基本微信小程序的小学生兴趣延时班预约小程序 - 2

    项目介绍随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱小学生兴趣延时班预约小程序的设计与开发被用户普遍使用,为方便用户能够可以随时进行小学生兴趣延时班预约小程序的设计与开发的数据信息管理,特开发了小程序的设计与开发的管理系统。小学生兴趣延时班预约小程序的设计与开发的开发利用现有的成熟技术参考,以源代码为模板,分析功能调整与小学生兴趣延时班预约小程序的设计与开发的实际需求相结合,讨论了小学生兴趣延时班预约小程序的设计与开发的使用。开发环境开发说明:前端使用微信微信小程序开发工具:后端使用ssm:VU

  3. ruby-on-rails - 使用 HTTParty 的非常基本的 Rails 4.1 API 调用 - 2

    Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"

  4. ruby-on-rails - Rails 基本 Base64 身份验证 - 2

    我正在尝试复制此GETcurl请求:curl-D--XGET-H"Authorization:BasicdGVzdEB0YXByZXNlYXJjaC5jb206NGMzMTg2Mjg4YWUyM2ZkOTY2MWNiNWRmY2NlMTkzMGU="-H"Content-Type:application/json"http://staging.example.com/api/v1/campaigns在Ruby中,通过电子邮件+apikey生成身份验证:auth="Basic"+Base64::encode64("test@example.com:4c3186288ae23fd9661c

  5. Simulink方法总结和避坑指南(一)——Simulink入门与基本调试方法 - 2

    文章目录一、项目场景二、基本模块原理与调试方法分析——信源部分:三、信号处理部分和显示部分:四、基本的通信链路搭建:四、特殊模块:interpretedMATLABfunction:五、总结和坑点提醒一、项目场景  最近一个任务是使用simulink搭建一个MIMO串扰消除的链路,并用实际收到的数据进行测试,在搭建的过程中也遇到了不少的问题(当然这比vivado里面的debug好不知道多少倍)。准备趁着这个机会,先以一个很基本的通信链路对simulink基础和相关的debug方法进行总结。  在本篇中,主要记录simulink的基本原理和基本的SISO通信传输链路(QPSK方式),计划在下篇记

  6. ruby - 是否有 Rack::Session::Cookie 用法的基本示例? - 2

    我找不到任何使用Rack::Session::Cookie的简单示例,并且希望能够将信息存储在cookie中,并在以后的请求中访问它并让它过期.这些是我能找到的唯一示例:HowdoIset/getsessionvarsinaRackapp?http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html这是我得到的:useRack::Session::Cookie,:key=>'rack.session',:domain=>'foo.com',:path=>'/',:expire_after=>2592000,:secret=

  7. ruby - 具有基本身份验证和 gzip 的 em-http 流挂起 - 2

    我正在尝试使用GnipPowerTrackAPI,这需要我使用基本身份验证连接到JSON的HTTPS流。我觉得这应该是相当微不足道的,所以我希望一些比我聪明的ruby​​ist可以指出我明显的错误。这是我的ruby​​1.9.3代码的相关部分:require'eventmachine'require'em-http'require'json'usage="#{$0}"abortusageunlessuser=ARGV.shiftabortusageunlesspassword=ARGV.shiftGNIP_STREAMING_URL='https://stream.gnip.com:4

  8. micropython复现经典单片机项目(二)可视化音频 频谱解析(基本搞定) - 2

    本人是音乐爱好者,从小就特别喜欢那个随着音乐跳动的方框效果,就是这个:arduino上一大把对,我忍你很久了,我就想用mpy做,全网没有,行我自己研究。果然兴趣是最好的老师,我之前有篇博客专门讲音频,有兴趣的可以回顾一下。提到可视化频谱,必然绕不开fft,大学学过这玩意,当时一心玩,老师讲的一个字都么听进去,网上教程简略扫了一下,大该就是把时域转频域的工具,我大mpy居然没有fft函数,奶奶的,先放着。音频信息如何收集?第一种傻瓜式的ADC,模拟转数字,原始粗暴,第二种,I2S库,我之前博客有讲过,数据是PCM编码。然后又去学PCM编码,一学豁然开朗,舒服,以代码为例:audio_in=I2S

  9. ruby - 是否可以在 Sinatra 中重写基本 URL? - 2

    是否可以重写基本URL?例如而不是www.host.com/使用www.host.com/blah/作为一个基本的url等等:get'/'do...end适用于www.host.com/blah/我可以附加到我的所有路线'/blah/..'但任何gem等。也将无法工作。这可以在Rails中轻松完成,我也想在Sinatra中使用它。 最佳答案 我为此使用了Rack中间件rack-rewrite我对此很满意:)useRack::Rewritedorewrite%r{^/\w{2}/utils},'/utils'rewrite%r{^/\

  10. ruby - 在公共(public)基本路线之上构建路线? - 2

    我有一个共同的基本路径;说:get/base我需要执行基本身份验证并为该路径下的所有子调用工作。说:get/base/foo和get/base/bar。查看http://www.sinatrarb.com/intro.html#Helpers建议我应该能够通过使用助手来做到这一点。我正在查看pass帮助程序并在文档中触发新路由下使用call。但是,我读到的另一个建议是使用正则表达式IE%r{/base/?:(path)?}或类似的动态路由。那么:def'/base'#dosomefunkybasicauthstuffhere#toworkwithallrequesttothiscomm

随机推荐