如何将 X 和 Y 转换为经纬度 [英] how to Convert X and Y to lat and long

查看:58
本文介绍了如何将 X 和 Y 转换为经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 IK_TEMP 的表,它包含名为 data, range 的列.

I have a table called IK_TEMP and it contains columns called data, range .

        String sql = "SELECT DATA, RANGE FROM IK_TEMP";

        try (Connection conn = this.connect();
             Statement stmt  = conn.createStatement();
             ResultSet rs    = stmt.executeQuery(sql)){

            // loop through the result set
            while (rs.next()) {
                System.out.println(rs.getString("DATA") +  "\t" + 
                                   rs.getBytes("RANGE"));
               fromBytes(rs.getBytes("RANGE"));
            }

RANGE 字段(二进制/BLOB)字段已使用来自 arcGIS 的二进制进行编码并保存在数据库中.http://www.geopackage.org/spec120/#gpb_format

The RANGE field(binary / BLOB) field is already encoded using binary from arcGIS and saved in the Database. http://www.geopackage.org/spec120/#gpb_format

我想使用 java 解码这个 RANGE 字段.

I want to decode this RANGE field using java.

这里我尝试了 fromBytes 方法

Here I have tried with fromBytes method

public void fromBytes(byte[] bytes) {
            this.bytes = bytes;

            ByteReader reader = new ByteReader(bytes);

            // Get 2 bytes as the magic number and validate
            String magic = null;
            try {
                magic = reader.readString(2);
            } catch (UnsupportedEncodingException e) {
                throw new GeoPackageException(
                        "Unexpected GeoPackage Geometry magic number character encoding: Expected: "
                                + GeoPackageConstants.GEOMETRY_MAGIC_NUMBER);
            }
            if (!magic
                    .equals(GeoPackageConstants.GEOMETRY_MAGIC_NUMBER)) {
                throw new GeoPackageException(
                        "Unexpected GeoPackage Geometry magic number: "
                                + magic
                                + ", Expected: "
                                + GeoPackageConstants.GEOMETRY_MAGIC_NUMBER);
            }

            // Get a byte as the version and validate, value of 0 = version 1
            byte version = reader.readByte();
            if (version != GeoPackageConstants.GEOMETRY_VERSION_1) {
                throw new GeoPackageException(
                        "Unexpected GeoPackage Geometry version: "
                                + version
                                + ", Expected: "
                                + GeoPackageConstants.GEOMETRY_VERSION_1);
            }

            // Get a flags byte and then read the flag values
            byte flags = reader.readByte();
            int envelopeIndicator = readFlags(flags);
            reader.setByteOrder(byteOrder);

            // Read the 5th - 8th bytes as the srs id
            srsId = reader.readInt();

            // Read the envelope
            envelope = readEnvelope(envelopeIndicator, reader);

            // Save off where the WKB bytes start
            wkbGeometryIndex = reader.getNextByte();

            // Read the Well-Known Binary Geometry if not marked as empty
            if (!empty) {
                geometry = GeometryReader.readGeometry(reader);
            }

        }

我在 geometry 对象中得到 xy 坐标和几何类型,但是我怎么能从中得到纬度和经度

I am getting x and y coordinates and geometryType in geometry object, But how can I get lat and long from this

在他们在 JS 中给出的一个例子中 参考.

In one of the example they have given in JS reff.

for item in (GeometryDataXYValue)!{
        let xValue = item.paths?.ofX
        let yValue = item.paths?.ofY

        //recieve x y point
        currentPoint = AGSPoint(x: xValue!, y: yValue!, spatialReference: AGSSpatialReference.webMercator()) 

        //convert to lat long by AGSSpatialReference.wgs84()

       if  let aReference = AGSGeometryEngine.projectGeometry(currentPoint!, to: AGSSpatialReference.wgs84()) as? AGSPoint {
            currentPoint = aReference
        }
    }
    var long:Double = currentPoint!.x
    var lat: Double = currentPoint!.y
    print("value long lat =  \(long , lat)")
}

但我想要在 java 中进行相同的转换.这是另一个例子

But I want the same conversion in java. This is another example

示例

推荐答案

您可以使用 geotools 库获取经纬度.

You can get the lat and long by using the geotools library.

Coordinate coordinate = new Coordinate(geometry.getEnvelope().getMaxX(), geometry.getEnvelope().getMaxY());
            MathTransform transform;
            Coordinate targetGeometry;
            double latitude;
            double longitude;
            try {

            //sourceCRS is to convert the X and Y coordinates to lat long
            CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:25832");
            CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

                transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
                targetGeometry = JTS.transform(coordinate, coordinate, transform);
                latitude = targetGeometry.getX();
                longitude = targetGeometry.getY();
            } catch (FactoryException | TransformException e) {
                e.printStackTrace();
            }
        }

这篇关于如何将 X 和 Y 转换为经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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