当前位置:首页>活动 >

Geotools基本增删改查Feature-天天观天下

时间:2023-04-24 04:16:32    来源:博客园


【资料图】

postgis依赖

    org.geotools    gt-main    27.2    org.geotools    gt-jdbc-postgis    27.2

创建连接JDBCDataStore

Map 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连接参数

ParameterDescription
dbtypeMust be the string postgis
hostMachine name or IP address to connect to
portPort number to connect to, default 5432
schemaThe database schema to access
databaseThe database to connect to
userUser name
passwdPassword
loose bboxFlag controlling loose bbox comparisons, default is true
preparedStatementsFlag controlling whether prepared statements are used, default is false
encode functionsFlag controlling if some common functions can be encoded into their SQL equivalent

连接池参数

ParameterDescription
max connectionsMaximum number of connection the pool will hold at any time, default is 10
min connectionsMinimum number of connection the pool will hold at any time, default is 1
connection timeoutMaximum number of second the pool will wait when trying to obtain a connection, default is 20 seconds
validate connectionsFlag controlling if the pool should validate connections when a new connection is obtained
Max open prepared statementsMaximum 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 idlePeriodically test if the connections are still valid also while idle in the pool
Time between evictor runsNumber of seconds between idle object evictor runs. The default value is 300 seconds.
Min evictable timeNumber of seconds a connection needs to stay idle before the evictor starts to consider closing it
Evictor tests per runNumber 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));//nullPropertyIsGreaterThan 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,toWKTFunction 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 propNames 字段名列表 *  * startIndex 起始位 * maxFeatures 最大条数 * sortField 排序字段名 * */ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);//返回字段列List 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 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-天天观天下

Geotools,Feature,JDBCDataStore,Filter

2023-04-24

天天实时:错误734是怎么回事_错误734

1、错误734,ppp链接控制协议终止。2、宽带提供商ISP(也就是运营商)的BAS上面的地址池不够用了,用户无法

2023-04-24

中药龙骨的功效与作用_中药龙骨是什么 天天新要闻

1、中文名称:龙骨英文名称:keel;OsDraconis(拉);bonefossilofbigmammal

2023-04-24

每日看点!(梦兄弟)救赎

半夜有感而发写的,文笔为0可能有点刀=(本故事发生在Killer和color私奔后———————————————

2023-04-24

辨别真假玉石的简单方法_鉴别玉石的简单方法 要闻

1、玉石的成分主要是硅酸盐①观光泽:真玉器无论半透明或不透明,都有温润光泽,内部夹有少量杂质或呈棉絮

2023-04-24

百项豪华标配,全新林肯冒险家打造跃级舒适体验

中期改款的全新一代林肯冒险家(参数|询价)在上海车展亮相上亮相,吸引无数逛展者的目光。新车不仅配备了大

2023-04-24

全球速递!无偿捐献热血 用爱点亮生命

22日,天津纯公益志愿服务队第50次组织无偿献血活动。三年来,该服务队的志愿者积极参加无偿献血活动,累计

2023-04-23

兰州新区首个新建保障性住房项目8月底竣工 世界动态

兰州新区首个新建保障性住房项目8月底竣工

2023-04-23

8-0!希金斯连胜八局迎史诗级开局,巫师狂轰三破百或轻松进八强

8-0!希金斯连胜八局迎史诗级开局,巫师狂轰三破百或轻松进八强,底袋,轰出,威尔逊,撞球运动员,约翰·希金斯

2023-04-23

清风头条丨古丈:出实招见真功 让廉洁清风吹拂茶乡

古丈县委坚决扛起清廉建设政治责任,强化“四责”协同,健全制度机制,明确方法路径,凝聚工作合力,进一步

2023-04-23

X 关闭

X 关闭