当前位置:网站首页>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 language final without failing (Part 1)
- JS returned content is encoded by Unicode
- Ansible ad-hoc 临时命令
- Questions about database: database attachment
- ctfshow 大赛原题 680-695
- Machine learning notes: time series decomposition STL
- Is the course of digging money reliable and safe to open an account?
- C语言 一元多项式求导
- 对深度网络模型量化工作的总结
- Varnish foundation overview 4
猜你喜欢
随机推荐
Sentinel source code analysis Part 8 - core process - sphu Entry current limiting execution
Pytoch modifies the hook source code to obtain per layer output parameters (with layer name)
【PyTorch实战】生成对抗网络GAN:生成动漫人物头像
Mysql 监控
对深度网络模型量化工作的总结
Interface Association of postman
[mrctf2020]ezpop-1 | PHP serialization
Questions about database: database attachment
【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
Mysql 监控3
Difference between test plan and test plan
The first technology podcast month will be launched soon
Varnish 基础概览10
C language number prime
Conversion between opencv and image (valid for pro test)
Cookie加密13
js Array.from()的5个便捷应用
Quality management of functional modules of MES management system
Cookie加密15 登录加密
Embedded exit (review and release)









