Java+proj4j implements coordinate system conversion according to EPSG encoding

Scenes

Java+GeoTools implements coordinate system conversion of WKT data according to EPSG encoding:

Java+GeoTools implements coordinate system conversion of WKT data according to EPSG encoding - Programmer Sought

The coordinate system conversion is implemented using GeoTools above.

Vue+Openlayers+proj4 implements coordinate system conversion:

Vue+Openlayers+proj4 realizes coordinate system conversion - Programmer Sought

And I talked about using proj4js in Vue to realize the coordinate system conversion of the front end.

In addition, proj4j can also be used to convert different coordinate system data according to epsg encoding in the java backend.

Note:

Blog:
Overbearing rogue temperament blog_CSDN Blog-C#, Architecture Road, Blogger in SpringBoot

accomplish

1、proj4j

Java side of the project.4 library for coordinate reprojection.

https://github.com/locationtech/proj4j

Add project dependencies


        <!-- https://mvnrepository.com/artifact/org.locationtech.proj4j/proj4j -->
        <dependency>
            <groupId>org.locationtech.proj4j</groupId>
            <artifactId>proj4j</artifactId>
            <version>1.2.3</version>
        </dependency>

2. Create a new conversion class, for example, implement EPSG:4524 to EPSG:2334 as above

import org.locationtech.proj4j.*;

public class HelloProj4J {
    public static void main(String[] args) {
        CRSFactory crsFactory = new CRSFactory();
        //源坐标系统
       
        //根据投影字符串参数获取坐标系统
        String SourceCRS= "4524";<BR>       StringSourceCRS_params="+proj=tmerc +lat_0=0 +lon_0=108 +k=1 +x_0=36500000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
        CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);

        //根据名称获取坐标系统
        //CoordinateReferenceSystem source = crsFactory.createFromName("epsg:4524");

        //目标坐标系统
       
        根据投影字符串参数获取坐标系统
        String TargetCRS= "2334";<BR>       StringTargetCRS_params="+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=20500000 +y_0=0 +ellps=IAU76 +units=m +no_defs";
        CoordinateReferenceSystem  target = crsFactory.createFromParameters(TargetCRS, TargetCRS_params);
       
        //根据名称获取坐标系统
        //CoordinateReferenceSystem  target =  crsFactory.createFromName("epsg:2334");

        //定义转换类
        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
        CoordinateTransform transform = ctf.createTransform(source, target);

        //坐标系转换
        ProjCoordinate projCoordinate = new ProjCoordinate(37360817.569479, 5127237.510467304);
        //ProjCoordinate projCoordinate = new ProjCoordinate(119.0632442, 45.7414338);
        transform.transform(projCoordinate, projCoordinate);
        System.out.println("转换后x:"+projCoordinate.x);
        System.out.println("转换后y:"+projCoordinate.y);
    }
}

Run the above code and compare it with the conversion effect of the epsg website

EPSG.io: Coordinate Systems Worldwide

3. Note that there are two ways to obtain the coordinate system here

One is directly through the name

CoordinateReferenceSystem source = crsFactory.createFromName("epsg:4524");

One is by projecting string parameters and custom names

        String SourceCRS = "4524";
       StringSourceCRS_params="+proj=tmerc +lat_0=0 +lon_0=108 +k=1 +x_0=36500000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
        CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);

Methods and rules for obtaining projection strings

Refer to the proj4js article above.

Remove the +type=crs part from the parameter of proj.4 obtained from epsg.io

Intercept to +no_defs and the previous part.

The origin of the rules is based on the comparison of the converted parameters in the examples in the document with the same parameters on the official website.

Otherwise proj4j will prompt: type parameter is not supported

 

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/130391544