GIS obtains the projected coordinate system EPSG to which the projected coordinates belong according to the projected coordinate points

What is EPSG?

EPSG (The European Petroleum Survey Group, official website http://www.epsg.org/ ) maintains a dataset of spatial reference objects, the SRID (Spatial Reference System Identifier) ​​of the spatial reference system in the OGC standard and the spatial reference system ID of EPSG consistent.

EPSG was established in 1986 and was "reorganized" in 2005 as The OGP Surveying and Positioning Committee, which maintains and publishes the dataset parameters of the coordinate reference system and coordinate transformation descriptions. The dataset is widely accepted and used, and is distributed through a Web publishing platform, while providing storage files for Microsoft Access databases, and databases such as mySQL, Oracle, and PostgreSQL are also available through SQL script files.

The organization publishes a dataset of coordinate reference systems and maintains dataset parameters of coordinate reference systems, as well as descriptions of coordinate transformations. The dataset encodes coordinate reference systems collected worldwide.


Code

First, introduce relevant geotools dependencies into the pom file


        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>24.3</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>24.3</version>
        </dependency>

Create a new CRSUtil tool class, the code is as follows:



import lombok.extern.slf4j.Slf4j;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;

import org.opengis.geometry.Envelope;
import org.opengis.referencing.crs.CoordinateReferenceSystem;


/**
 * @author tarzan
 */
@Slf4j
public class CRSUtil {

	public static void main(String[] args) {
	    String code=getCRS(39559047.43599, 3977293.67339,CRSConstant.CGCS_2000);
		System.out.println(code);

	}

	static 	int[] codes={
			//WGS 84
			3857,
			//CGCS2000
			4512, 4511, 4510, 4509, 4508, 4507, 4506, 4505, 4504, 4503, 4502, 4501, 4500, 4499, 4498, 4497, 4496, 4495, 4494, 4493, 4492, 4491,
			4554, 4553, 4552, 4551, 4550, 4549, 4548, 4547, 4546, 4545, 4544, 4543, 4542, 4541, 4540, 4539, 4538, 4537, 4536, 4535, 4534,
			4533, 4532, 4531, 4530, 4529, 4528, 4527, 4526, 4525, 4524, 4523, 4522, 4521, 4520, 4519, 4518, 4517, 4516, 4515, 4514, 4513,
			//Beijing 1954
			21463, 21462, 21461, 21460, 21459, 21458, 21457, 21456, 21455, 21454, 21453,
			21423, 21422, 21421, 21420, 21419, 21418, 21417, 21416, 21415, 21414, 21413,
			2442, 2441, 2440, 2439, 2438, 2437, 2436, 2435, 2434, 2433, 2432, 2431, 2430, 2429, 2428, 2427, 2426, 2425, 2424, 2423, 2422,
			2421, 2420, 2419, 2418, 2417, 2416, 2415, 2414, 2413, 2412, 2411, 2410, 2409, 2408, 2407, 2406, 2405, 2404, 2403, 2402, 2401,
			//New Beijing  
			4589, 4588, 4587, 4586, 4585, 4584, 4583, 4582, 4581, 4580, 4579,
			4578, 4577, 4576, 4575, 4574, 4573, 4572, 4571, 4570, 4569, 4568,
			4822, 4812, 4800, 4799, 4798, 4797, 4796, 4795, 4794, 4793, 4792, 4791, 4790, 4789, 4788, 4787, 4786, 4785, 4784, 4783, 4782,
			4781, 4780, 4779, 4778, 4777, 4776, 4775, 4774, 4773, 4772, 4771, 4770, 4769, 4768, 4767, 4766, 4756, 4755, 4754, 4753, 4753,
			//Xian 80
			2348, 2347, 2346, 2345, 2344, 2343, 2342, 2341, 2340, 2339, 2338,
			2337, 2336, 2335, 2334, 2333, 2332, 2331, 2330, 2329, 2328, 2327,
			2390, 2389, 2388, 2387, 2386, 2385, 2384, 2383, 2382, 2381, 2380, 2379, 2378, 2377, 2376, 2375, 2374, 2373, 2372, 2371, 2370,
			2369, 2368, 2367, 2366, 2365, 2364, 2363, 2362, 2361, 2360, 2359, 2358, 2357, 2356, 2355, 2354, 2353, 2352, 2351, 2350, 2349,
	};


	/**
	 * 方法描述: 获取投影坐标系
	 *
	 */
	public static String getCRS(double x, double y,String crsName) {
		for (int code : codes) {
			try {
				String epsgCode="EPSG:"+code;
				CoordinateReferenceSystem crs = CRS.decode(epsgCode);
				Envelope envelope=CRS.getEnvelope(crs);
				ReferencedEnvelope projectedBounds = new ReferencedEnvelope(envelope.getMinimum(1),envelope.getMaximum(1),envelope.getMinimum(0),envelope.getMaximum(0),crs);
				if(projectedBounds.contains(x,y)){
					String nameCode=crs.getName().getCode();
					if(nameCode.startsWith(crsName)){
						return epsgCode;
					}
				}
			}catch (Exception e){
				log.error(e.getMessage());
			}
		}
		return null;
	}






}

Run the main method test in an editor such as idea, and the output is as follows:

	public static void main(String[] args) {
	    String code=getCRS(39559047.43599, 3977293.67339,CRSConstant.CGCS_2000);
		System.out.println(code);
	}

Substitute x and y for your projected coordinates, specifying a geographic coordinate system. 

I tried it in the middle, and directly obtained the geographic coordinate system according to the projected coordinates. Through the dialogue between Baidu and AI, I found that there are only projected coordinates, even if there are multiple ones, the geographic coordinates cannot be obtained.

Recently, another problem has been found that the conversion parameters of Beijing 54 coordinates read in the gt-epsg-hsql package do not correspond to the coordinates surrounded by the geographical range and the epsg official website. Beijing 54, the read information and the projection parameters of the new Beijing coordinates Number one model. I tried to update the hsql database in the middle, but found that the operation was troublesome, and it was also a problem to download the epsg data set from the official website. The foreign website was very slow, and the mailbox registration was unsuccessful. In the end, I gave up, and temporarily planned to judge the design of Beijing 54 coordinates and convert the coordinates. Replace with wtk or proj4j string.

 

Additional code sample:

 geotools use

	public static void main(String[] args) throws FactoryException {
		String wkt = "PROJCS[\"Beijing 1954 / 3-degree Gauss-Kruger zone 25\",GEOGCS[\"Beijing 1954\",DATUM[\"Beijing_1954\",SPHEROID[\"Krassowsky 1940\",6378245,298.3],TOWGS84[12.646,-155.176,-80.863,0,0,0,0]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4214\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",75],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",25500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"2401\"]]";
		CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
		System.out.println(crs.toWKT());
	}

 The proj4j package uses

The pom file introduces dependencies:

        <dependency>
            <groupId>org.locationtech.jts</groupId>
            <artifactId>jts-core</artifactId>
            <version>1.17.1</version>
        </dependency>

        <dependency>
            <groupId>org.locationtech.proj4j</groupId>
            <artifactId>proj4j</artifactId>
            <version>1.2.3</version>
        </dependency>

code example


import org.locationtech.proj4j.*;

public class Proj4jTest {

		public static void main(String[] args) {
			CRSFactory crsFactory = new CRSFactory();
			//源坐标系统
			//根据投影字符串参数获取坐标系统
			String SourceCRS= "2442";
			String SourceCRS_params="+proj=tmerc +lat_0=0 +lon_0=135 +k=1 +x_0=500000 +y_0=0 +ellps=krass +towgs84=12.646,-155.176,-80.863,0,0,0,0 +units=m +no_defs";
			CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);
			//目标坐标系统
			//根据投影字符串参数获取坐标系统
			String TargetCRS= "4326";
			String TargetCRS_params="+proj=longlat +datum=WGS84 +no_defs";
			CoordinateReferenceSystem  target = crsFactory.createFromParameters(TargetCRS, TargetCRS_params);


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

			//坐标系转换
			ProjCoordinate projCoordinate = new ProjCoordinate(-2088963.21,4174250.75);
			transform.transform(projCoordinate, projCoordinate);
			System.out.println("转换后x:"+projCoordinate.x);
			System.out.println("转换后y:"+projCoordinate.y);
		}

}

related articles

WKIDs for commonly used geographic and projected coordinate systems 

Using the gis tool station of epsg official data, domestic access is faster

"geotools official website"

"epsg official website"

Guess you like

Origin blog.csdn.net/weixin_40986713/article/details/130605308