java实现离线正反向地理编码代码

时间: 2023-11-13 19:19:31 浏览: 42
Java实现离线正反向地理编码需要用到第三方库,比如Geocoder、Geotools、OSM Nominatim等。以下是一个使用Geotools实现离线正向地理编码的示例代码: ```java import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.feature.simple.SimpleFeatureImpl; import org.geotools.geometry.jts.JTSFactoryFinder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.geometry.coordinate.Position; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; import org.locationtech.jts.io.ParseException; import org.locationtech.jts.io.WKTReader; public class GeoCoder { private ShapefileDataStore dataStore; private SimpleFeatureType featureType; private MathTransform transform; private Map<Long, String> id2Name; public GeoCoder(File shapefile) throws IOException { dataStore = new ShapefileDataStore(shapefile.toURL()); featureType = dataStore.getSchema(); CoordinateReferenceSystem sourceCRS = featureType.getCoordinateReferenceSystem(); CoordinateReferenceSystem targetCRS = org.geotools.referencing.crs.DefaultGeographicCRS.WGS84; transform = org.geotools.referencing.CRS.findMathTransform(sourceCRS, targetCRS, true); id2Name = new HashMap<>(); SimpleFeatureIterator iterator = dataStore.getFeatureSource().getFeatures().features(); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); long id = (Long) feature.getAttribute("ID"); String name = (String) feature.getAttribute("NAME"); id2Name.put(id, name); } iterator.close(); } public String getAddress(double lat, double lon) throws IOException, TransformException { GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Point point = geometryFactory.createPoint(new Coordinate(lon, lat)); Geometry targetGeometry = null; try { targetGeometry = JTS.transform(point, transform); } catch (TransformException e) { e.printStackTrace(); } if (targetGeometry != null) { String wktPoint = targetGeometry.toText(); String[] coords = wktPoint.replace("POINT (", "").replace(")", "").split(" "); double x = Double.parseDouble(coords[0]); double y = Double.parseDouble(coords[1]); Geometry searchGeometry = geometryFactory.createPoint(new Coordinate(x, y)); try { SimpleFeatureIterator iterator = dataStore.getFeatureSource().getFeatures().features(); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); if (searchGeometry.intersects(geometry)) { long id = (Long) feature.getAttribute("ID"); String name = id2Name.get(id); return name; } } iterator.close(); } catch (Exception e) { e.printStackTrace(); } } return null; } } ``` 示例代码使用了一个Shapefile作为地理编码数据源,通过读取Shapefile的属性表生成一个id到名称的映射表,并且可以通过给定的经纬度坐标返回对应的名称。

相关推荐

最新推荐

recommend-type

在Unity中实现动画的正反播放代码

主要介绍了在Unity中实现动画的正反播放代码,非常的实用,这里推荐给大家,希望大家能够喜欢。
recommend-type

CS5265+VL170支持Type-C母座正反插转HDMI投屏4K60HZ拓展方案设计电路

CS5265+VL170方案设计电路,Type-C母座正反插转HDMI投屏方案设计,Type-C转HDMI 4k60HZ拓展方案电路参考 CS5265是一款高性能TYPEC/DP1.4到HDMI2.0转换器,设计用于连接USB-C或DP1.4到HDMI2.0转换器。
recommend-type

matlab用基本运算符实现福利叶正反变换

本文使用基本运算符,以离散化,周期延拓,离散信号福利叶变换的方式实现福利叶正反变换。以便深入理解福利叶变换在计算机中的实现过程,处理手段,以及优缺点,并给在嵌入式系统使用福利叶变换提供建议。
recommend-type

STC89C51 简单时钟

STC89C51 简单时钟,叫你从基础开始学习单片机,
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?

![MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?](https://www.finebi.com/wp-content/uploads/2019/11/FineBI%E8%A1%8C%E4%B8%9A%E9%A9%BE%E9%A9%B6%E8%88%B1-1024x510.png) # 1. MATLAB归一化概述 归一化是一种数据预处理技术,用于将数据缩放到特定范围内,从而消除不同特征之间的尺度差异。在MATLAB中,有各种归一化方法可用于不同类型的数据和应用程序。 归一化的主要目的是: - 提高模型的训练效率和准确性,通过消除特征之间的尺度差异,使模型能够更有效地学习
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

Linux系统常用操作命令大全手册

附件是Linux系统常用操作命令大全手册,是 markdown格式,其中覆盖了Linux系统管理、文件操作、网络配置等多个方面,都是日常工作中非常常用的命令,欢迎大家下载学习使用!
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB归一化实战指南:从零到一,轻松提升模型性能

![MATLAB归一化实战指南:从零到一,轻松提升模型性能](https://pic1.zhimg.com/80/v2-fd366800ef0bdf29c804ce25c0276778_1440w.webp) # 1. 归一化的理论基础** 归一化是一种数据预处理技术,旨在将数据缩放到特定范围内,通常是[0, 1]或[-1, 1]。通过归一化,可以消除数据量纲上的差异,使不同特征具有可比性,从而提高机器学习模型的性能。 归一化背后的基本原理是,在训练机器学习模型时,模型的学习过程会受到数据分布的影响。如果数据分布不均匀,某些特征可能具有较大的值域,而其他特征的值域较小。这会导致模型在训练过