说说geotools中坐标转换那点事
概述:
本文说说geotools中坐标转换的那点事情,以WGS84和web墨卡托相互转换为例。
效果:
转换前
转换后
单个Geometry转换
实现代码:
package com.lzugis.geotools;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.io.WKTWriter;
public class ProjectTrans {
private static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
private static WKTReader reader = new WKTReader( geometryFactory );
private static WKTWriter write = new WKTWriter();
static ProjectTrans proj = new ProjectTrans();
final String strWKTMercator = "PROJCS[\"World_Mercator\","
+ "GEOGCS[\"GCS_WGS_1984\","
+ "DATUM[\"WGS_1984\","
+ "SPHEROID[\"WGS_1984\",6378137,298.257223563]],"
+ "PRIMEM[\"Greenwich\",0],"
+ "UNIT[\"Degree\",0.017453292519943295]],"
+ "PROJECTION[\"Mercator_1SP\"],"
+ "PARAMETER[\"False_Easting\",0],"
+ "PARAMETER[\"False_Northing\",0],"
+ "PARAMETER[\"Central_Meridian\",0],"
+ "PARAMETER[\"latitude_of_origin\",0],"
+ "UNIT[\"Meter\",1]]";
/**
* 经纬度转WEB墨卡托
* @param geom
* @return
*/
public Geometry lonlat2WebMactor(Geometry geom){
try{
//这里是以OGC WKT形式定义的是World Mercator投影,网页地图一般使用该投影
// CoordinateReferenceSystem crsTarget = CRS.parseWKT(strWKTMercator);
CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:3857");
// 投影转换
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crsTarget);
return JTS.transform(geom, transform);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public void projectShape(String inputShp, String outputShp){
try {
//源shape文件
ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(inputShp).toURI().toURL());
//创建目标shape文件对象
Map<String, Serializable> params = new HashMap<String, Serializable>();
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
params.put(ShapefileDataStoreFactory.URLP.key, new File(outputShp).toURI().toURL());
ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
// 设置属性
SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
//下面这行还有其他写法,根据源shape文件的simpleFeatureType可以不用retype,而直接用fs.getSchema设置
CoordinateReferenceSystem crs = CRS.parseWKT(strWKTMercator);
ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), crs));
//设置writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写记录
SimpleFeatureIterator it = fs.getFeatures().features();
try {
while (it.hasNext()) {
SimpleFeature f = it.next();
SimpleFeature fNew = writer.next();
fNew.setAttributes(f.getAttributes());
Geometry geom = proj.lonlat2WebMactor((Geometry)f.getAttribute("the_geom"));
fNew.setAttribute("the_geom", geom);
}
}
finally {
it.close();
}
writer.write();
writer.close();
ds.dispose();
shapeDS.dispose();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 工具类测试方法
* @param args
*/
public static void main(String[] args){
String wktPoint = "POINT(100.02715479879 33.462715497945)";
String wktLine = "LINESTRING(108.32803893589 41.306670233001,99.950999898452 25.84722546391)";
String wktPolygon = "POLYGON((100.02715479879 32.168082192159,102.76873121104 37.194305614622,107.0334056301 34.909658604412,105.96723702534 30.949603786713,100.02715479879 32.168082192159))";
try {
long start = System.currentTimeMillis();
Geometry geom1 = (Geometry) reader.read(wktPoint);
Geometry geom1T = proj.lonlat2WebMactor(geom1);
Geometry geom2 = (Geometry) reader.read(wktLine);
Geometry geom2T = proj.lonlat2WebMactor(geom2);
Geometry geom3 = (Geometry) reader.read(wktPolygon);
Geometry geom3T = proj.lonlat2WebMactor(geom3);
System.out.println(write.write(geom1T));
System.out.println(write.write(geom2T));
System.out.println(write.write(geom3T));
String inputShp = "D:\\data\\beijing\\China4326.shp",
outputShp = "D:\\data\\beijing\\China3857.shp";
proj.projectShape(inputShp, outputShp);
System.out.println("坐标转换完成,共耗时"+(System.currentTimeMillis() - start)+"ms");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关引用:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-extension</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${geotools.version}</version>
</dependency>
---------------------------------------------------------------------------------------------------------------
技术博客
CSDN:http://blog.csdn.NET/gisshixisheng
博客园:http://www.cnblogs.com/lzugis/
在线教程
http://edu.csdn.Net/course/detail/799
Github
https://github.com/lzugis/
联系方式
q q:1004740957
e-mail:niujp08@qq.com
公众号:lzugis15
Q Q 群:452117357(webgis)
337469080(Android)
说说geotools中坐标转换那点事的更多相关文章
- 开发中关于Git那些事
如果你想精通Git,直接到 Git官网 把这本ProGit掌握已足以Pro Git 此文主要介绍一切开发中常用的git命令和一些配置技巧(诸如git别名配置,log打印技巧,版本回退以及分支管理等). ...
- 开发中关于Git那些事(续:Git变基)
其实上一篇写的内容仅仅是Git的冰山一角,如果你认为Git就是简简单单的几行命令,那只能说明你还没有真正了解Git这个强大的内容寻址文件系统.这篇文章,还是接着介绍一些实用但是很少有人知晓的一些命令, ...
- geotools中泰森多边形的生成
概述 本文讲述如何在geotools中生成泰森多边形,并shp输出. 泰森多边形 1.定义 泰森多边形又叫冯洛诺伊图(Voronoi diagram),得名于Georgy Voronoi,是由一组由连 ...
- 探讨 JS 的面向对象中继承的那些事
最近学了 JS 的面向对象,这篇文章主要是探讨 JS 的面向对象中继承的那些事. JS中继承的特点: 1.子类继承父类: 2.子类可以用父类的方法和属性 3.子类的改变可以不影响父类 下面用一个例子来 ...
- django基础之day09,Forms组件在程序中做了哪些事? 校验数据、渲染标签、展示信息
******************************* Forms组件 *************************************************** Forms组件在 ...
- 「译」forEach循环中你不知道的3件事
前言 本文925字,阅读大约需要7分钟. 总括: forEach循环中你不知道的3件事. 原文地址:3 things you didn't know about the forEach loop in ...
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
- vue项目中遇到的那些事。
前言 有好几天没更新文章了.这段实际忙着做了一个vue的项目,从 19 天前开始,到今天刚好 20 天,独立完成. 做vue项目做这个项目一方面能为工作做一些准备,一方面也精进一下技术. 技术栈:vu ...
- JavaScript中继承的那些事
引言 JS是一门面向对象的语言,但是在JS中没有引入类的概念,之前特别疑惑在JS中继承的机制到底是怎样的,一直学了JS的继承这块后才恍然大悟,遂记之. 假如现在有一个“人类”的构造函数: functi ...
随机推荐
- Python+Django+Bootstrap 框架环境搭建
1.安装python和pip(python.pip安装自行百度,pip是一个安装和管理 Python 包的工具) 2.配置python环境变量(python和scripts目录都需要配置) 3.安装D ...
- Python集合方法整理(Day9)
#作用:去重,关系运算, #定义: 知识点回顾 可变类型是不可hash类型 不可变类型是可hash类型 #定义集合: 集合:可以包含多个元素,用逗号分割, 集合的元素遵循三个原则: 1:每个元素必须是 ...
- beego——获取参数
1.获取参数 我们经常需要获取用户传递的数据,包括Get.POST等方式的请求,beego里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string ...
- day3-set集合
set是一个无序且不重复的元素集合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- js原型 作用域
了解JavaScript原型链之前首先肯定要知道什么是原型. JavaScript中,原型是一个对象,通过原型可以实现属性的继承.既然原型是一个对象,那么任何一个对象都可以称为原型吗?是,记住它.什么 ...
- UEFI引导修复
一.用bcbboot自动修复 我们建议大家启动64位8PE,用它带的bcdboot来修复. (一)指定esp分区修复 环境为64位8PE,bios/uefi启动进入下都可以 1.启动64位8PE,并用 ...
- Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)
配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的 hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...
- windows技巧--一次关闭所有资源管理器目录,文件夹目录
每天开机工作一段时间以后,你可能会和我一样,打开了很多的文件目录,于是一个一个的点窗口关闭.于是想有没有一次关闭所有目录的办法~~咚咚咚,经过一番寻觅,下面是我找到的办法 新建bat文件 close_ ...