Coordinate Conversion, EPSG:4326 Conversion to Golden Coordinate Course

Keywords: Python JSON Excel Amap PostgreSQL

Here we first introduce several coordinate systems:

1.WGS84: International coordinate system, a geodetic coordinate system, is also the coordinate system widely used in GPS Global Satellite Positioning System.
2.GCJ02: Mars coordinate system is a coordinate system of Geographic Information System formulated by the State Bureau of Surveying and Mapping of China. The WGS84 coordinate system is encrypted.

Sometimes it is necessary to convert shape data into Gaud coordinate data. Because most of the current data are WGS84 international coordinate system, it is necessary to convert 84 coordinates into GCJ02 Mars coordinate system by using API provided by Gaud map.

Step 1: Preparing data

Method 1:

Some points are extracted randomly from the shape graph, and according to the size of the shape graph, if the figure is large, it is suggested to extract more points. Generally, it is chosen evenly around the inflection point and figure.

The longitude and latitude of coordinate points are obtained after the point extraction is completed. Arcgis computing tools can be used to obtain x,y values. Where the red box is painted, it should be noted that the longitude and latitude represented by decimal system should be selected.

After the coordinate extraction is completed, a new EXCEL table is created, and the values of X and y are copied and pasted into the first two columns of the excel table.

Method 2:

The shape data is stored in postgresql, and the function provided by postgis is used to randomly extract points from the shape, and its x,y values are extracted. This method is random selection, so the selected points are not necessarily uniformly distributed around the graph and at the inflection point.

1 SELECT ST_X(geom), ST_Y(geom) from(
2     SELECT
3         (ST_Dump (ST_GeneratePoints (nanjing.geom, 20))).geom AS geom,
4          md5((random()*random())::text) as id,
5          random()*1000 as val
6          FROM nanjing where name = 'Pukou District')
7          k1

Step 2: Execute the following python code to invoke the Gaud API for coordinate conversion

 1 import xlrd
 2 from xlutils.copy import copy
 3 from urllib import request
 4 import json
 5 
 6 
 7 class ToGd():
 8     def __init__(self, key, coordsys='gps', output='JSON'):
 9         self.key = key  # Golden Applied key
10         self.coordsys = coordsys  # Original coordinate system, default selection gps,This corresponds to the coordinate system of 4326.
11         self.output = output  # Set Gao De api Data return type, optional JSON and xml
12         self.file_path = input('Please enter your file path')  # Get the file path, which is newly created above excel File name
13 
14     def split_li(self, locations_li):
15         # Split coordinate list
16         li = list()
17         for location in locations_li:
18             location_li = location.split(',')
19             # print(location_li)
20             li.append(location_li)
21         # print(li)
22         return li
23 
24     def locations(self):
25         # Read the data in the file and return it locations String and original coordinate list
26         f = xlrd.open_workbook(self.file_path)
27         index = f.sheet_names()[0]
28         sheet = f.sheet_by_name(index)
29         nrows = sheet.nrows
30         data_str = ''
31         old_locations_li = list()
32         for i in range(nrows):
33             if i == (nrows - 1):
34                 data_li = str(sheet.row_values(i)[0]) + ',' + str(sheet.row_values(i)[1])
35             else:
36                 data_li = str(sheet.row_values(i)[0]) + ',' + str(sheet.row_values(i)[1]) + '|'
37             data_str += data_li
38             old_locations_li.append(sheet.row_values(i))
39         return data_str, old_locations_li
40 
41     def make_response(self, locations):
42         # Construct request url,And get the response data, return to the list of coordinates after splitting
43         url = 'https://restapi.amap.com/v3/assistant/coordinate/convert?locations=%s&coordsys=%s&output=%s&key=%s' % \
44               (locations, self.coordsys, self.output, self.key)
45         # The return value is json
46         resp_json = request.urlopen(url)
47         # json Converting to a dictionary
48         resp_dict = json.loads(resp_json.read().decode())
49         # Extraction of coordinates after conversion to Goethe coordinates
50         locations_str = resp_dict.get('locations')
51         # String segmentation
52         locations_li = locations_str.split(';')
53         new_locations_li = self.split_li(locations_li)
54         return new_locations_li
55 
56     def save(self, old_locations_li, new_locations_li):
57         # Preservation excel or txt file
58         # new_file = copy(f)
59         # new_sheet = new_file.get_sheet(0)
60         # for m in range(len(locations_li)):
61         #     for n in range(len(locations_li[m])):
62         #         new_sheet.write(m, n + 2, locations_li[m][n])
63         # file_path = self.file_path + '2'
64         # new_file.save(file_path)
65         file_path = 'result.txt'
66         with open(file_path, 'w', encoding='utf-8')as f:
67             for m in range(len(new_locations_li)):
68                 # for n in range(len(new_locations_li[m])):
69                 f.write(str(old_locations_li[m][0]))
70                 f.write(' ')
71                 f.write(str(old_locations_li[m][1]))
72                 f.write(' ')
73                 f.write(str(new_locations_li[m][0]))
74                 f.write(' ')
75                 f.write(str(new_locations_li[m][1]))
76                 f.write('\n')
77 
78     def run(self):
79         data_str, old_locations_li = self.locations()
80         new_locations_li = self.make_response(data_str)
81         self.save(old_locations_li, new_locations_li)
82 
83 
84 if __name__ == '__main__':
85     test = ToGd('154c586add07ef456b90b079935f47a4')  # Enter the key of Goethe's application, instantiate the object
86     test.run()

The code is written by myself, but the level is not high. Please understand.

To execute the above code, you need to apply for the key of the Gaud application. The relevant application method can be found at https://lbs.amap.com/dev/key.

After the code is executed, a result.txt file will be generated locally. Next, we need to use this file for spatial correction.

Step 3: Space correction

In Arcgis, the data that need to be coordinate transformed is edited, the toolbar of spatial correction is opened, and the connection file is input. The connection file is the result.txt file from the second step.

You can click View Link Table to see the error value. If the error value is too large, you need to redo the connection file.

Select the data to be corrected.

Click Calibration.

 

After correction, shape data matching Gaud map can be obtained.

The method mentioned above can only be used in small-scale test without accuracy evaluation. If large-scale commercial use is needed, other methods need to be considered.

Posted by phaseonemedia on Tue, 17 Sep 2019 01:40:48 -0700