当前位置:网站首页>Geotools wkt coordinate system conversion
Geotools wkt coordinate system conversion
2022-06-30 01:33:00 【liuccn】
Reference article :
java geotools Coordinate transformation
Maven in GeoTools The introduction of - Maven Of repository And mirror The relationship between
background
Use geotools Yes WKT String for coordinate system conversion .
pom Package introduction
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
<!-- gt-epsg-hsql This is epsgcode Of , Make sure to introduce , Otherwise, the coordinate system cannot be found -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>20.0</version>
</dependency>
Code implementation
/** * Single geom transformation * geotools In the installation package of x,y The coordinates are reversed , therefore , Direct use geomtry transformation , The exported data is wrong , First general * x,y Change the coordinates of . therefore , I choose to assemble geomtry. * @param geom * @return */
public static Geometry geometryTransform2(Geometry geom, int sourceEPSGCode,int targetEPSGCode ){
try{
//CoordinateReferenceSystem There are two ways to generate ,EPSG and WKT, However , I found that the coordinate system I needed prj The contents of the file are used to convert the message “ non-existent .... What kind of ..”
// So using EPSG, Even if EPSG, Of the exported converted projection file prj Still wrong ,arcgis Can't recognize . Of course , In the same coordinate system prj The contents of the file are the same , Just replace it with something else .
CoordinateReferenceSystem crsresource = CRS.decode("EPSG:"+ epsgCode);
CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:"+ targetEPSGCode);
// Projection conversion
MathTransform transform = CRS.findMathTransform(crsresource, crsTarget,true);
String wktString = geom.toString();
log.debug(" Before conversion WKT character string :" + wktString);
// take WKTString Regular matching of coordinates in , After the projection conversion, the corresponding coordinates in the string are replaced
Pattern pattern = compile("[1-9]\\d*\\.?\\d*\\s[1-9]\\d*\\.?\\d*");
Matcher matcher = pattern.matcher(wktString);
while (matcher.find()){
String cordStr = matcher.group();
String[] cord = cordStr.split("\\s+");
double x= Double.parseDouble(cord[0]);
double y= Double.parseDouble(cord[1]);
// Use coordinate when , Results dest Medium x The number of digits in the coordinates is a scientific count , So it is not recommended to use
/*Coordinate source = new Coordinate(y, x); Coordinate dest = new Coordinate(); JTS.transform(source,dest, transform); String[] str = dest.toString().substring(1, dest.toString().length() - 1).split(",");*/
String point="POINT(" + y + " " + x + ")";
Geometry pointGeom = reader.read(point);
Geometry resGeom = JTS.transform(pointGeom, transform);
String[] str = resGeom.toString().substring(resGeom.toString().lastIndexOf("(")+1, resGeom.toString().length() - 1).split("\\s+");
String cordStr2 = str[1]+" "+str[0];
wktString = wktString.replaceAll(cordStr,cordStr2);
}
log.debug(" Converted WKT character string :" + wktString);
Geometry read = reader.read(wktString);
return read;
} catch (Exception e) {
log.debug(" Error in coordinate system conversion :" + e.getMessage());
return null;
}
}
test
public static void main(String[] args) {
String wktStr = "POLYGON((40535321.97750524 3276858.489317663,40534838.475054964 3276742.437636584,40534885.506722644 3276050.3168770154,40535410.02098725 3276621.3582205614,40535321.97750524 3276858.489317663))";
Geometry geometry = reader.read(wktStr);
Geometry targetGeometry = WKTUtil.geometryTransform2(geometry,4528,4326);
System.out.println(targetGeometry.toString());
}
边栏推荐
- C语言 素数对猜想
- Unity2D--给动画添加关键帧并绑定事件
- Pytorch中transforms的用法整理
- 传统微服务框架如何无缝过渡到服务网格 ASM
- Write this number in C
- postman 之接口关联
- JS returned content is encoded by Unicode
- Varnish foundation overview 6
- STC89C52 single chip microcomputer simple calculator design and code demonstration
- [recommendation system] concise principle and code implementation of user based collaborative filtering
猜你喜欢

JS prototype and prototype chain (Lantern Festival meal)

首届·技术播客月开播在即

Sklearn notes: make_ Blobs generate clustering data

C language output integer in another format

R language linear regression model fitting diagnosis outliers analysis of domestic gas consumption and calorie examples with self-test questions

Cookie encryption 8

【机器学习Q&A】数据抽样和模型验证方法、超参数调优以及过拟合和欠拟合问题

js返回内容被unicode编码

STC89C52 single chip microcomputer simple calculator design and code demonstration

Ansible ad-hoc temporary command
随机推荐
Module import reload method
Mysql 监控5
Some thoughts on small program subcontracting and verification of uiniapp subcontracting optimization logic
Cookie encryption 13
Machine learning notes: time series decomposition STL
JS content confusion, return content encryption
The Web3 era is coming? Inventory of five Web3 representative projects | footprint analytics
Understand the module function of MES management system
Mechanical --nx2007 (UG) -- gap analysis (interference inspection)
C语言 一元多项式求导
Difference between test plan and test plan
Pytorch中transforms的用法整理
Varnish foundation overview 4
JS returned content is encoded by Unicode
js Array.from()的5个便捷应用
js返回内容被unicode编码
C语言 继续(3n+1)猜想
模块导入reload方法
Design and implementation of spark offline development framework
STC89C52 single chip microcomputer simple calculator design and code demonstration