Geotools基本增删改查Feature

打印 上一主题 下一主题

主题 885|帖子 885|积分 2655

postgis依赖
  1. <dependency>
  2.     <groupId>org.geotools</groupId>
  3.     <artifactId>gt-main</artifactId>
  4.     <version>27.2</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.geotools</groupId>
  8.     <artifactId>gt-jdbc-postgis</artifactId>
  9.     <version>27.2</version>
  10. </dependency>
复制代码
创建连接JDBCDataStore
  1. Map<String, String> params = Map.of(
  2.     PostgisNGDataStoreFactory.HOST.key, host,
  3.     PostgisNGDataStoreFactory.PORT.key, port,
  4.     PostgisNGDataStoreFactory.DATABASE.key, database,
  5.     PostgisNGDataStoreFactory.SCHEMA.key, schema,
  6.     PostgisNGDataStoreFactory.USER.key, user,
  7.     PostgisNGDataStoreFactory.PASSWD.key, passwd,
  8.     PostgisNGDataStoreFactory.DBTYPE.key, dbtype
  9. );
  10. JDBCDataStore jdbcDataStore = (JDBCDataStore)DataStoreFinder.getDataStore(params);
复制代码
JDBCDataStore连接参数

ParameterDescriptiondbtypeMust be the string postgishostMachine name or IP address to connect toportPort number to connect to, default 5432schemaThe database schema to accessdatabaseThe database to connect touserUser namepasswdPasswordloose bboxFlag controlling loose bbox comparisons, default is truepreparedStatementsFlag controlling whether prepared statements are used, default is falseencode functionsFlag controlling if some common functions can be encoded into their SQL equivalent连接池参数

ParameterDescriptionmax connectionsMaximum number of connection the pool will hold at any time, default is 10min connectionsMinimum number of connection the pool will hold at any time, default is 1connection timeoutMaximum number of second the pool will wait when trying to obtain a connection, default is 20 secondsvalidate connectionsFlag controlling if the pool should validate connections when a new connection is obtainedMax 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 disableTest while idlePeriodically test if the connections are still valid also while idle in the poolTime 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 itEvictor 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有多个子类,实现了许多类型的过滤器,包括简单的属性比较和空间查询。
  1. // 普通字段
  2. FilterFactory ff = CommonFactoryFinder.getFilterFactory();
  3. /**
  4. * field 字段名
  5. * value 条件值
  6. * geometry 条件几何体
  7. *
  8. *
  9. * matchCase 是否区分大小写,默认true-区分
  10. * MatchAction(实现MultiValuedFilter的会有),匹配逻辑
  11. * MatchAction.ANY-任何一个满足,默认值
  12. * MatchAction.ALL-全部满足
  13. * MatchAction.ONE-只有一个满足
  14. * */
  15. PropertyIsEqualTo equal = ff.equal(ff.property(field), ff.literal(value), true);//等于
  16. PropertyIsLike like = ff.like(ff.property(field), "%keywords%");//模糊匹配
  17. PropertyIsNotEqualTo notEqualTo = ff.notEqual(ff.property(field), ff.literal(value));//不等于
  18. PropertyIsNull aNull = ff.isNull(ff.property(field));//null
  19. PropertyIsGreaterThan greater = ff.greater(ff.property(field), ff.literal(value));// 大于
  20. PropertyIsGreaterThanOrEqualTo greaterOrEqual = ff.greaterOrEqual(ff.property(field), ff.literal(value));// 大于等于
  21. PropertyIsLessThan less = ff.less(ff.property(field), ff.literal(value));//小于
  22. PropertyIsLessThanOrEqualTo lessOrEqual = ff.lessOrEqual(ff.property(field), ff.literal(value));//小于等于
  23. PropertyIsBetween between = ff.between(ff.property(field), ff.literal(value), ff.literal(value));//在...之间
  24. During during = ff.during(ff.property(field), ff.literal(value));//在时间期间
  25. Before before = ff.before(ff.property(field), ff.literal(value));//在时间之前
  26. After after = ff.after(ff.property(field), ff.literal(value));//在时间之后
  27. // Geometry字段
  28. FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2();
  29. Beyond beyond = ff2.beyond(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry), 100.0, Units.METRE.name);// 图层几何字段超出给定几何100米距离的
  30. Contains contains = ff2.contains(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 图层几何字段包含给定几何
  31. Within within = ff2.within(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 图层几何字段被给定几何包含
  32. Intersects intersects = ff2.intersects(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 图层几何字段与给定几何相交
  33. Disjoint disjoint = ff2.disjoint(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 图层几何字段与给定几何不相交
  34. Touches touches = ff2.touches(ff2.property(featureSource.schema.geometryDescriptor.localName), ff2.literal(geometry));// 图层几何字段与给定几何相切
  35. // filter集合的逻辑关系,and并,or或,not非
  36. And and = ff.and(List.of(equal,like,beyond));//
  37. Or or = ff.or(List.of(notEqualTo,greater,contains));
  38. Not not = ff.not(during);
  39. // Function的实现类具体实现函数,name-函数名,例如:min,strReplace,toWKT
  40. Function function = ff.function(name,expr1,exprN);
  41. PropertyName property = ff.property(field);
  42. Literal v = ff.literal(value);
  43. Function min = ff.function("min", property, v);
  44. PropertyName property = ff.property(field);
  45. Literal search = ff.literal("search");
  46. Literal replace = ff.literal("replace");
  47. Literal all = ff.literal( true );
  48. Function replace = ff.function("strReplace", new Expression[]{property,search,replace,all});
  49. PropertyName property = ff.property(featureSource.schema.geometryDescriptor.localName);
  50. Function toWKT = ff.function("toWKT", property);
复制代码
查询
  1. /**
  2. * tableName 表名
  3. * filter 过滤器
  4. * List<String> propNames 字段名列表
  5. *
  6. * startIndex 起始位
  7. * maxFeatures 最大条数
  8. * sortField 排序字段名
  9. * */
  10. ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
  11. //返回字段列
  12. List<PropertyName> propertyNames = propNames.stream().map(ff::property).collect(Collectors.toList());
  13. Query query = new Query(tableName,filter,propertyNames);
  14. int count = featureSource.getCount(query);//计数
  15. // 分页,倒序
  16. query.setStartIndex(startIndex);
  17. query.setMaxFeatures(maxFeatures);
  18. query.setSortBy(new SortByImpl(ff.property(sortField), SortOrder.DESCENDING));
  19. ContentFeatureCollection collection = featureSource.getFeatures(query);
  20. SimpleFeatureIterator iterator = collection.features();
  21. // SimpleFeatureIterator必须关闭,否则会造成内存泄漏
  22. iterator.close();
复制代码
新增
  1. /**
  2. * tableName 图层名
  3. * fieldName1 字段名
  4. * fieldValue1 字段值
  5. **/
  6. ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
  7. SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
  8. SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(store.getSchema());
  9. featureBuilder.set(fieldName1,fieldValue1);
  10. featureBuilder.set(fieldNameN,fieldValueN);
  11. SimpleFeature feature = featureBuilder.buildFeature(null);
  12. ListFeatureCollection featureCollection = new ListFeatureCollection(store.getSchema(), List.of(feature));
  13. List<FeatureId> addFeatures = store.addFeatures(featureCollection);
复制代码
删除
  1. /**
  2. *
  3. * typeName 图层名
  4. * fliter 过滤条件
  5. *
  6. */
  7. ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
  8. SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
  9. store.removeFeatures(filter);
复制代码
修改
  1. /**
  2. *
  3. * typeName 图层名
  4. * names 修改字段名数组
  5. * values 修改值数组
  6. * fliter 过滤条件
  7. * names和values顺序保持一致
  8. */
  9. ContentFeatureSource featureSource = (ContentFeatureSource) jdbcDataStore.getFeatureSource(tableName);
  10. SimpleFeatureStore store = (SimpleFeatureStore)featureSource;  // write access!
  11. store.modifyFeature(names, values, filter)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

盛世宏图

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表