当前位置: 首页 > news >正文

GeoTools 工厂设计模式

前言

使用GeoTools开发时有必要了解其工厂设计模式,作为软件开发核心设计模式,其设计思想具有普遍性和研究性。明白方法原理有助于提高开发效率,达到事半功倍的效果。

1. 工厂模式

工厂模式(Factory Pattern)是面向对象中编程中最常用的设计模式之一,使用工厂不仅可以批量创建对象,而且创建对象的过程与使用对象的过程分离。有利于降低代码的耦合度。

GeoTools的设计也采用了工厂设计模式的思想,用于创建地理空间数据处理中的各种对象。使得其面对各种地理数据格式时,可以灵活拓展。

下面介绍以下GeoTools中常见的工厂模型,主要包括数据存储工厂、几何对象工厂以及坐标系工厂等。

2. 数据存储工厂

GeoTools 数据存储工厂非常丰富,主要包括数据库存储工厂和文件存储工厂两大类,基本都存储处 org.geotools.data目录下。

(1)DataStoreFactory可以用于连接各种数据源,如数据库和文件。

  • 连接数据库:
// 连接PostGIS数据库
Map<String, Object> pgParams = new HashMap<>();
pgParams.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
pgParams.put(PostgisNGDataStoreFactory.HOST.key, "localhost");
pgParams.put(PostgisNGDataStoreFactory.PORT.key, "5432");
pgParams.put(PostgisNGDataStoreFactory.DATABASE.key, "geodata");
pgParams.put(PostgisNGDataStoreFactory.USER.key, "postgres");
pgParams.put(PostgisNGDataStoreFactory.PASSWD.key, "123456");
pgParams.put(PostgisNGDataStoreFactory.SCHEMA.key, "public"); // 明确指定schema
pgParams.put(PostgisNGDataStoreFactory.EXPOSE_PK.key, true);  // 暴露主键// DataStoreFinder 自动发现对应工厂
DataStore pgDataStore = DataStoreFinder.getDataStore(pgParams);
  • 连接文件:
String provinceLocation = "E:\data\基础数据\行政区\省级行政区\省级行政区\省.shp";
File file = new File(provinceLocation);// 创建 Shapefile 存储器
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
params.put("charset", "UTF-8");// DataStoreFinder 自动发现对应工厂
DataStore dataStore = DataStoreFinder.getDataStore(params);

也可以用使用FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);

(2)ShapefileDataStoreFactory用于创建Shapefile存储器。

// 创建 Shapefile 存储器
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
params.put("charset", "UTF-8");ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore =  (ShapefileDataStore) dataStoreFactory.createDataStore(params);

(3)PostgisNGDataStoreFactory用于创建PostGIS空间数据库连接。

// 连接PostGIS数据库
Map<String, Object> pgParams = new HashMap<>();
pgParams.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
pgParams.put(PostgisNGDataStoreFactory.HOST.key, "localhost");
pgParams.put(PostgisNGDataStoreFactory.PORT.key, "5432");
pgParams.put(PostgisNGDataStoreFactory.DATABASE.key, "geodata");
pgParams.put(PostgisNGDataStoreFactory.USER.key, "postgres");
pgParams.put(PostgisNGDataStoreFactory.PASSWD.key, "123456");
pgParams.put(PostgisNGDataStoreFactory.SCHEMA.key, "public"); // 明确指定schema
pgParams.put(PostgisNGDataStoreFactory.EXPOSE_PK.key, true);  // 暴露主键// DataStoreFinder 自动发现对应工厂
DataStore pgDataStore = DataStoreFinder.getDataStore(pgParams);
if (pgDataStore == null) {PostgisNGDataStoreFactory factory = new PostgisNGDataStoreFactory();if (!factory.canProcess(params)) {System.err.println("数据库参数不满足工厂要求");}if (!factory.isAvailable()) {System.err.println("数据库工厂类不可用");}throw new RuntimeException("数据库连接失败,请检查?");
}

3. 几何对象工厂

GeometryFactory 用来为要素创建几何属性,如点、线、面对象。

GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
// 构造点
Point point = geometryFactory.createPoint(new Coordinate(longitude,latitude));

4. 坐标系工厂

CRSFactory工厂用于创建坐标参考系统。

// 获取数据源坐标系统
CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();// 获取地图坐标系统
CoordinateReferenceSystem mapCrs  = map.getCoordinateReferenceSystem();// 定义坐标系统
CoordinateReferenceSystem wgs84 = CRS.decode("EPSG:4326");

5. 样式工厂

StyleFactory工厂用于创建图层样式

StyleFactory style = CommonFactoryFinder.getStyleFactory();
// 创建红色填充
Fill fill = style.createFill(ff.literal(Color.RED));

6. 过滤器工厂

FilterFactory工厂用于创建数据过滤条件,通常用于空间查询

// 创建数据过滤器
FilterFactory factory = CommonFactoryFinder.getFilterFactory(null);
// 获取几何属性字段名称
String geometryPropertyName = featureSource.getSchema().getGeometryDescriptor().getLocalName();Filter filter = null;
switch (queryType.toLowerCase()) {case "intersects":filter = factory.intersects(factory.property(geometryPropertyName),factory.literal(geometry));break;case "contains":filter = factory.contains(factory.property(geometryPropertyName),factory.literal(geometry));break;case "disjoint":filter = factory.disjoint(factory.property(geometryPropertyName),factory.literal(geometry));break;
}

文章转载自:
http://brazilein.sxnf.com.cn
http://antidote.sxnf.com.cn
http://assimilability.sxnf.com.cn
http://caliphate.sxnf.com.cn
http://benomyl.sxnf.com.cn
http://cenogenesis.sxnf.com.cn
http://carnose.sxnf.com.cn
http://acquiesce.sxnf.com.cn
http://annihilative.sxnf.com.cn
http://burmese.sxnf.com.cn
http://bemud.sxnf.com.cn
http://auctioneer.sxnf.com.cn
http://chivalrous.sxnf.com.cn
http://aubrietia.sxnf.com.cn
http://allometric.sxnf.com.cn
http://anadem.sxnf.com.cn
http://airward.sxnf.com.cn
http://allhallows.sxnf.com.cn
http://beetroot.sxnf.com.cn
http://aimlessly.sxnf.com.cn
http://bonaire.sxnf.com.cn
http://chlorite.sxnf.com.cn
http://chipped.sxnf.com.cn
http://behaviourism.sxnf.com.cn
http://angelologic.sxnf.com.cn
http://benzomorphan.sxnf.com.cn
http://capersome.sxnf.com.cn
http://chivalry.sxnf.com.cn
http://acceptably.sxnf.com.cn
http://cephalochordate.sxnf.com.cn
http://www.dtcms.com/a/280377.html

相关文章:

  • 传输协议和消息队列
  • AR眼镜:重塑医学教育,开启智能教学新时代
  • 同步辐射XAFS和XRD协同用于高熵合金的研究应用
  • 香港站群服务器租用:为什么需要选择不同C类IP?
  • python的广东省家庭旅游接待信息管理系统
  • k8s之Attach 和 Mount
  • C++回顾 Day7
  • k8s之Snapshots 详解
  • Linux C IO多路复用
  • 静态补丁脚本 - 修改 libtolua.so
  • Unity音游开发全指南:模板与免费资源高效构建节奏游戏
  • Ubuntu 22.04 安装 mysql-server服务端
  • docker拉取nacos镜像失败
  • golang语法-----标准化输入输出
  • 渗透测试技术_Nessus工具(三):输出报告
  • 构建 Go 可执行文件镜像 | 探索轻量级 Docker 基础镜像(我应该选择哪个 Docker 镜像?)
  • STM32小实验三--让蜂鸣器响起来
  • Pytorch中张量的索引和切片使用详解和代码示例
  • CSS的初步学习
  • 用语音识别芯片驱动TFT屏幕还有链接蓝牙功能?
  • cursor使用mcp连接mysql数据库,url方式
  • java截取视频帧
  • c#进阶之数据结构(字符串篇)----String
  • C++中list各种基本接口的模拟实现
  • 【Java代码审计(2)】MyBatis XML 注入审计
  • 153.在 Vue 3 中使用 OpenLayers + Cesium 实现 2D/3D 地图切换效果
  • java中的接口
  • JavaScript 动态访问嵌套对象属性问题记录
  • HarmonyOS-ArkUI: Web组件加载流程1
  • 暴力破解:攻破系统的终极密钥