当前位置:网站首页>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());
}
边栏推荐
猜你喜欢
【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向
Mechanical --nx2007 (UG) -- gap analysis (interference inspection)
【论文写作】英文论文写作指南
Ctfshow competition original title 680-695
Understand the module function of MES management system
Cookie encryption 9
JS returned content is encoded by Unicode
The Web3 era is coming? Inventory of five Web3 representative projects | footprint analytics
Embedded test template
cookie加密8
随机推荐
Ansible ad-hoc temporary command
Interview summary
Varnish 基础概览4
js返回内容被unicode编码
Difference between test plan and test plan
JS recursive summation 1-100
C语言 数组元素循环右移问题
Conversion between opencv and image (valid for pro test)
Kubernetes 核心对象概览详解
Some thoughts on small program subcontracting and verification of uiniapp subcontracting optimization logic
MySQL monitoring 3
Cookie encryption 13
Sentinel source code analysis Part 8 - core process - sphu Entry current limiting execution
How to seamlessly transition from traditional microservice framework to service grid ASM
Module import reload method
[535. encryption and decryption of tinyurl]
App test related tools
[binary tree] maximum binary tree II
cookie加密8
Database application