墨卡托坐标 EPSG:3857 坐标系。投影坐标系,EPSG:3857 的数据一般是这种的。 [12914838.35,4814529.9] ,看上去相对数值较大。不利于存储,比较占内存。
WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
EPSG:4326 的数据一般是这种的。 [22.37,114.05] 。利于存储,可读性高
// 2437 GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
另一种方式,我未使用
blog.csdn.net/u010410697/…
Geotools是一个java类库,它提供了很多的标准类和方法来处理空间数据,同时这个类库是构建在OGC标准之上的,是OGC思想的一种实现。而OGC是国际标准,所以geotools将来必定会成为开源空间数据处理的主要工具,目前的大部分开源软件,如udig,geoserver等,对空间数据的处理都是由geotools来做支撑。而其他很多的web服务,命令行工具和桌面程序都可以由geotools来实现。
中文文档:max.book118.com/html/2019/0…
工具包+相关文档下载:download.csdn.net/download/ab…
JTS(Java Topology Suite) Java拓扑套件,是Java的处理地理数据的API,它提供以下功能:
实现了OGC关于简单要素SQL查询规范定义的空间数据模型
一个完整的、一致的、基本的二维空间算法的实现,包括二元运算(例如touch和overlap)和空间分析方法(例如intersection和buffer)
一个显示的精确模型,用算法优雅的解决导致dimensional collapse(尺度坍塌–专业名词不知道对不对,暂时这样译)的情况。
健壮的实现了关键计算几何操作
提供著名文本格式的I/O接口
JTS是完全100%由Java写的
JTS支持一套完整的二元谓词操作。二元谓词方法将两个几何图形作为参数,返回一个布尔值来表示几何图形是否有指定的空间关系。它支持的空间关系有:相等(equals)、分离(disjoint)、相交(intersect)、相接(touches)、交叉(crosses)、包含于(within)、包含(contains)、覆盖/覆盖于(overlaps)。同时,也支持一般的关系(relate)操作符。relate可以被用来确定维度扩展的九交模型(DE-9IM),它可以完全的描述两个几何图形的关系。
locationtech.github.io/jts/
github.com/locationtec…
maven仓库
看了下vividsolutions于2015年就停在更新了,好像在哪儿看过,jts由vividsolutions变为的locationtech,一个爹 应该是。选locationtech这个就完事了
JTS提供了以下空间数据类型:
多数的空间数据模型都是遵从这个的。
private GeometryFactory geometryFactory = new GeometryFactory();
ByCoordinate坐标
Coordinate coord = new Coordinate(109.013388, 32.715519);
Point point = geometryFactory.createPoint( coord );
ByWKT
WKTReader reader = new WKTReader( geometryFactory );
Point point = (Point) reader.read("POINT (109.013388 32.715519)");
WKTReader reader = new WKTReader( geometryFactory );
MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");
Coordinate[] coords = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
LineString line = geometryFactory.createLineString(coords);
WKTReader reader = new WKTReader( geometryFactory );
LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");
Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
LineString line1 = geometryFactory.createLineString(coords1);
Coordinate[] coords2 = new Coordinate[] {new Coordinate(2
, 2), new Coordinate(2, 2)};
LineString line2 = geometryFactory.createLineString(coords2);
LineString[] lineStrings = new LineString[2];
lineStrings[0]= line1;
lineStrings[1] = line2;
MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
WKTReader reader = new WKTReader( geometryFactory );
MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");
LinearRing lr = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(0, 0), new Coordinate(0, 10), new Coordinate(10, 10), new Coordinate(10, 0), new Coordinate(0, 0)});
WKTReader reader = new WKTReader( geometryFactory );
Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
WKTReader reader = new WKTReader( geometryFactory );
MultiPolygon mpolygon = (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))");
LineString line = createLine();
Polygon poly = createPolygonByWKT();
Geometry g1 = geometryFactory.createGeometry(line);
Geometry g2 = geometryFactory.createGeometry(poly);
Geometry[] garray = new Geometry[]{g1,g2};
GeometryCollection gc = geometryFactory.createGeometryCollection(garray);
| 关系 | 解释 |
|---|
| 相等(Equals): | 几何形状拓扑上相等。 |
| 不相交(Disjoint): | 几何形状没有共有的点。 |
| 相交(Intersects): | 几何形状至少有一个共有点(区别于脱节) |
| 接触(Touches): | 几何形状有至少一个公共的边界点,但是没有内部点。 |
| 交叉(Crosses): | 几何形状共享一些但不是所有的内部点。 |
| 内含(Within): | 几何形状A的线都在几何形状B内部。 |
| 包含(Contains): | 几何形状B的线都在几何形状A内部(区别于内含) |
| 重叠(Overlaps): | 几何形状共享一部分但不是所有的公共点,而且相交处有他们自己相同的区域。 |
举例相交,其他类似
* 至少一个公共点(相交)
* @return
* @throws ParseException
public boolean intersectsGeo() throws ParseException{
WKTReader reader = new WKTReader( geometryFactory );
LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)");
Geometry interPoint = geometry1.intersection(geometry2);
System.out.println(interPoint.toText());
return geometry1.intersects(geometry2);
JTS支持基本的空间分析方法。空间分析方法使用一个或两个几何图形作为参数,返回一个新构造的几何图形。
| 分析 | 解释 |
|---|
| 缓冲区分析(Buffer) | 包含所有的点在一个指定距离内的多边形和多多边形 |
| 凸壳分析(ConvexHull) | 包含几何形体的所有点的最小凸壳多边形(外包多边形) |
| 交叉分析(Intersection) | A∩B 交叉操作就是多边形AB中所有共同点的集合 |
| 联合分析(Union) | AUB AB的联合操作就是AB所有点的集合 |
| 差异分析(Difference) | (A-A∩B) AB形状的差异分析就是A里有B里没有的所有点的集合 |
| 对称差异分析(SymDifference) | (AUB-A∩B) AB形状的对称差异分析就是位于A中或者B中但不同时在AB中的所有点的集合 |
1.先把坐标系都转成墨卡托
2.用jts计算距离
3.由于转成墨卡托坐标系,结果比实际偏大,要乘以系数
CoordinateReferenceSystem sourceCRS = CRS.decode("CRS:84");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
GeometryFactory GeometryFactory = new GeometryFactory();
Coordinate k96 = new Coordinate(117.41722, 31.975379);
Coordinate k97 = new Coordinate(117.426387, 31.970712);
Geometry line = JTS.transform(GeometryFactory.createLineString(new Coordinate[]{k96, k97}), transform);
double rate = Math.cos((Math.toRadians(k96.y) + Math.toRadians(k97.y)) / 2.000000);
System.out.println("rate: " + rate);
System.out.println(line.getArea());
System.out.println(line.getLength() * rate);
Coordinate tmp = new Coordinate(117.417145, 31.975465);
Geometry point = JTS.transform(GeometryFactory.createPoint(tmp), transform);
System.out.println(point.distance(line) * rate);
工具包+相关文档下载:download.csdn.net/download/ab…
偶然间发现可以使用jts中的工具可视化我们的WKT数据。
需要java环境
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
WKTReader reader = new WKTReader(gf);
Geometry line2 = reader.read("LINESTRING(0 0, 10 0, 10 10, 20 10)");
Coordinate c = new Coordinate(5, 5);
PointPairDistance ppd = new PointPairDistance();
DistanceToPoint.computeDistance(line2, c, ppd);
System.out.println(ppd.getDistance());
for (Coordinate cc : ppd.getCoordinates()) {
System.out.println(cc);
LinearRing lr = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,10), new Coordinate(10,10), new Coordinate(10,0), new Coordinate(0,0)});
LineString ls = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(5,-1), new Coordinate(5,11)});
Geometry intersectionPoints = lr.intersection(ls);
for(Coordinate c : intersectionPoints.getCoordinates()){
System.out.println(c.toString());
创建一个包含在设定距离内的所有点的面或多面(想要圆,但是没有。。。):
geometry = point
Geometry buffer = geometry.buffer( 2.0 )
已知:起点、终点、道路
求解:其在道路上的子轨迹
GeometryFactory GeometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(GeometryFactory);
Geometry geom = reader.read("LINESTRING(0 0, 10 0, 10 10, 20 10)");
LocationIndexedLine lil = new LocationIndexedLine(geom);
LinearLocation start = lil.indexOf(new Coordinate(8, 5));
LinearLocation end = lil.indexOf(new Coordinate(17, 10));
Geometry result = lil.extractLine(start, end);
System.out.println(result.toText());
LINESTRING (10 5, 10 10, 17 10)
已知:道路、在道路的距离
求解:到起点坐标距离的点
public static Coordinate lengthOnLineString2(Geometry roadLine, double length) {
LocationIndexedLine locationIndexedLine = new LocationIndexedLine(roadLine);
LinearLocation linearLocation = LengthLocationMap.getLocation(roadLine, length);
Coordinate result = locationIndexedLine.extractPoint(linearLocation);
return result;
blog.csdn.net/yatsov/arti…
geotools.org/
locationtech.github.io/jts/javadoc…
www.giserdqy.com/geoanalysis…
docs.geotools.org/latest/user…
docs.geotools.org/latest/user…
docs.geotools.org/latest/user…