怎么把HEXEWKB转换成纬度,经度(在Java中)? [英] How to convert HEXEWKB to Latitude, Longitude (in java)?

查看:464
本文介绍了怎么把HEXEWKB转换成纬度,经度(在Java中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从开放街道地图"项目下载了街道信息.此文件基本上是具有以下字段的CSV:

I downloaded Street information from the Open Street Map project. This file is basically a CSV with the following fields:

CSV fields
==============================================
1 : id; UniqueId of the street(always null for the moment)
2 : name; The name 
3 : location; The middle point of the street in HEXEWKB
4 : length ; Length of the street in meters
5 : countrycode; The iso3166 Alpha2 Code of the country 
6 : gid ; GlobalId (not use yet)
7 : type; The type of street (see bellow)
8 : oneway; Whether the street is a one way street
9 : shape; The delimitation of the street in HEXEWKB 

我找不到如何将此HEXEWKB字段隐藏起来对我有用的东西,例如纬度,经度.谁能给我一些关于这种格式如何工作的指示(或者更好的是,将我指向一些对我有用的java库)?

I cannot find how to covert this HEXEWKB field to something useful for me, like Latitude, Longitude. Can anyone give me some pointers on how this format works (or better, point me to some java lib that does that job for me)?

CSV中的示例行:

3343954 Blijde Inkomststraat    0101000020E6100000AECB9307F9D812400F2ADCE003704940  454.419285782969    NL      tertiary    t0102000020E610000007000000CD4301367BDB1240B59377C4D76F49402E7A02BC60DB12404F80176CD96F494061C8451042DB1240F5B814FCDB6F49405104279133DB12402AE0432EDD6F4940875C5FDA26DB12409434DA05DE6F494018DEF64E16D81240FCA2A9431370494049B258D471D61240839A6BE22E704940
3343967 Dagobertstraat  0101000020E6100000E40AAE6CD6DA1240941F95531C704940  216.34491093149 NL      residential f   0102000020E610000004000000F15C29159ED9124094FF2499307049406EDDCD531DDA1240C8E99040287049400CC2267C00DC12409672631F097049403A5739590FDC12400EA9FD3108704940
3343968 Bogaardenstraat 0101000020E6100000C0D7C68E7CD81240F550364044704940  125.953915569017    NL      residential f   0102000020E610000002000000224D614AC9D7124048B19245507049405F622CD32FD91240A2F0D93A38704940

推荐答案

您可以使用 JTS做到这一点.使用WBKReader可以很容易地从EWKB十六进制表示形式加载几何.

You can do that with JTS. It's easy to load the geometry from its EWKB Hexadecimal representation, using WBKReader.

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.io.WKBReader;

import java.util.Arrays;
import java.util.List;

public class Foo {
    public static void main(String... args) throws Exception {
        final List<String> points = Arrays.asList(
                "0101000020E6100000AECB9307F9D812400F2ADCE003704940",
                "0101000020E6100000E40AAE6CD6DA1240941F95531C704940",
                "0101000020E6100000C0D7C68E7CD81240F550364044704940"
        );
        final GeometryFactory gm = new GeometryFactory(new PrecisionModel(), 4326);
        final WKBReader wkbr = new WKBReader(gm);
        for (String point: points) {
            byte[] wkbBytes = wkbr.hexToBytes(point);
            final Geometry geom = wkbr.read(wkbBytes);
            System.out.printf("%s -> %s\n", point, geom.toText());
            // you can access longitude and latitude via
            double longitude = geom.getCoordinate().x;
            double latitude = geom.getCoordinate().y;
            // then do what you need to do with it
        }
    }
}

将打印:

0101000020E6100000AECB9307F9D812400F2ADCE003704940 -> POINT (4.7118874725301065 50.87511835813722)
0101000020E6100000E40AAE6CD6DA1240941F95531C704940 -> POINT (4.713708589670862 50.875864455999505)
0101000020E6100000C0D7C68E7CD81240F550364044704940 -> POINT (4.71141265 50.87708285)

您可能还知道JTS位于Maven Central上,因此很容易将依赖项添加到您的项目中.

You may also know that JTS is on maven central, so it's easy to add a dependency to your project.

<dependency>
    <groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.13</version> <!-- or RELEASE as stated in @RobAu comment below -->
</dependency>

至于魔术数字4326是地理坐标的通用SRID ,即经度/latitude在WGS84椭球上.

As for the magic number 4326 is a common SRID for geographic coordinates which is the longitude/latitude on the WGS84 spheroid.

您可以阅读此链接

You can read this link and that one about that number, and a nice introduction to all that stuff.

这篇关于怎么把HEXEWKB转换成纬度,经度(在Java中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆