Build your own Baidu map coordinate rectification database

Source: Internet
Author: User

Lishui Automobile Transportation Group Co., Ltd Information Center supine

GPS related development, without exception will be encountered coordinate rectification problem, that is, the received GPS coordinates, if the direct display to the electronic map, and the actual location has a large gap, must be in the GPS coordinates based on an offset, to correctly display, because the migration is non-linear, and the algorithm is confidential, Therefore, it is difficult to obtain the algorithm, the online common practice is to use the offset database, the principle is that a certain area of the offset is similar, then, According to 0.01 degrees or 0.001 degrees, and so on, the Chinese map into a number of areas, the GPS coordinates in which area, to take out the region offset, and GPS coordinates added to get map coordinates. China is very big, the database is also very large, there is little complete, even if there is, also need to pay, Baidu Map provides a correction interface, the introduction of GPs latitude and longitude, return map latitude, but access through the Web interface, obviously can not meet large-scale concurrent processing, so the use of local offset library relatively reliable. The latitude and longitude of Chinese mainland (including Hainan Island) is between longitude 73.5~135 degree and latitude 18~53.6, if press 1 degree a record, then record number is (53.6-18) * (135-73.5) = 2189.4, obviously, 1 degree span is too big, If 0.1 degrees, then need 2189.4*100 (10 squared) about 220,000 records, If it is 0.01 degrees, then need 21.894 million, already very large, and then accurate to 0.001, then, is 2.1894 billion, according to the measured, so high precision is not necessary, accurate to 0.01 degrees enough, that is, the Mainland China 1/ The 100-degree offset library has 21.894 million records, or a larger one, our vehicles are not in mainland China can be opened anywhere, such as vehicles will not open to the roof of residents, we actually use not much, you can refer to my other blog (click to open the link) to build a streamlined database, here, we use the Baidu map interface , create a complete offset database yourself. The results are as follows (one out of 10,000 degrees of latitude and longitude are used):

We first look at Baidu correction interface:

http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=120.434966&y=28.160923

x represents longitude, Y represents latitude, and the website returns the following information:

{"Error": 0, "x": "Mtiwljq0ntg3nzczotc1", "Y": "Mjgumtyznjm2mzm0nju5"}

This is a JSON data structure, error=0 expression of success, X represents the longitude after the correction, Y on behalf of the latitude after the deviation, by the latitude of the Base64 encryption, after restoration can be normally achieved, note that the interface each return results may be different, that is, we are always close to the real value, But it's never going to get the real value.

With this interface, we can loop through two times, longitude from 73.5 to 135 loops, increase by 0.01 degrees each time, and so on, so that we can automatically generate our own offset library.

We know that the transport speed of integers is much faster than floating point numbers, so we refer to the Department of GPS regulations, with one out of 10,000 degrees to describe latitude and longitude, that is, the latitude and longitude are multiplied by 1 million in the database. Obviously, a database table should have at least a longitude (int), latitude (int), longitude offset (smallint), latitude offset (smallint) several columns, due to the instability of the network, and can not guarantee a one-time each parse successfully, therefore, also need to add a column, when parsing failure, make a mark, For the next reparse. Here I write in C # to call the Baidu interface to parse the code for reference, parameters are one out of 10,000 degrees:

#region Query Baidu Correction coordinates

<summary>

Calculate Baidu Map coordinates

</summary>

<paramname= "Lng" > Original longitude </param>

<paramname= "Lat" > Original latitude </param>

<returns> Baidu Map Correction after the coordinates </returns>

Public Point Getbaiduposoff (intlng, int Lat)

{

Pointpos = new Point (0,0);

Restore to degree for interface use

DOUBLELNG = 0.000001 * Lng, lat = 0.000001 * LAT;

Try

{

StringUrl = string. Format ("Http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={0}&y={1}",

LNG, LAT);

Httpwebrequestrequest = (HttpWebRequest) webrequest.create (URL);

Request. Timeout = 5000;

Httpwebresponseresponse = (httpwebresponse) request. GetResponse ();

Streamstream = Response. GetResponseStream ();

Byte[]bytes = new byte[1024];

INTN = stream. Read (bytes, 0, 1024);

Response. Close ();

if (n < 2)

{

Showlog (String. Format ("Returns an incorrect length of longitude: {0}, latitude 3 degrees: {1}", LNG, LAT));

}

Else

{

strings = System.Text.Encoding.UTF8.GetString (bytes). Substring (1,n-2). Replace ("\", "");

foreach (String teamins. Split (', '))

{

String[] Infos = Team. Split (': ');

if (infos. Length < 2)

{

Showlog (String. Format ("Malformed, Longitude: {0}, Latitude: {1}", LNG, LAT));

Break

}

String strvalue = Infos[1];

Switch (Infos[0])

{

Case "Error":

if (strvalue!= "0")

Showlog (String. Format ("returned error number, Longitude: {0}, Latitude: {1}, error number: {2}", LNG, Lat,strvalue));

Break

Case "X":

{

Byte[] Outputb =convert.frombase64string (strvalue);

strvalue = Encoding.Default.GetString (OUTPUTB);

Pos. X = (int) Double. Parse (strvalue) * 1000000);

}

Break

Case "Y":

{

Byte[] Outputb =convert.frombase64string (strvalue);

strvalue = Encoding.Default.GetString (OUTPUTB);

Pos. Y = (int) Double. Parse (strvalue) * 1000000);

}

Break

}

}

}

}

catch (Exception ee)

{

Showlog (String. Format ("error, Longitude: {0}, latitude 3 degrees: {1}, error message: {2}", LNG, Lat,ee. message));

}

Returnpos;

}

#endregion

Incoming latitude and longitude (one out of 10,000 degrees), to get a point data structure, X represents the longitude after the correction (one out of 10,000 degrees), Y represents latitude, if x or y is 0, the resolution failed, you need to record the next reparse. After the correction of the coordinates minus the original coordinates, you get the offset value, you can save to the database.

For example:

INTNLNG = 85000000, Nlat = 28000000,nlngoffset=0,nlatoffset=0;

POINTPT = Getbaiduposoff (NLNG, Nlat);

Boolbsuccess = false;

if (Pt. X > 0 && Pt. Y > 0)

{

Nlngoffset = Pt. X-NLNG;

Nlatoffset = Pt. Y-nlat;

bsuccess = true;

}

In this way, we can put the original coordinates, offset values, successfully saved to the database.

With this database, the use of relatively simple, first of all, GPS coordinates (one out of 10,000 degrees) to 0.01 degrees of longitude (GPS coordinates/10000*10000), and from the database to obtain the offset value, and then the original GPS and coordinate offset added, the reference code is as follows:

int nlng = 85123456,nlat = 28123456;

Read offset from database

POINTPT = Getdboff (nlng/10000 * 10000, nlat/10000 * 10000);

Plus offset

NLNG + + pt. X

Nlat + = Nlat; Should say, the whole principle is very simple, the real difficulty is the time, measured, a second about can parse 4~5, Chice record probably need two months continuous work, I am afraid most people are impatient, to solve also very simple, multithreading + computer operations, each thread responsible for a paragraph, I call the computer resources more convenient, with 6 servers, each open two programs, one weeks not all resolved. If there is a need for friends, I uploaded to the csdn Download Center, after the specific links and so out, I gave in the comments, I am using SQL Server 2008 R2, considering that many people or SQL Server 2005, I back up to the SQL server2005 format, If you are still using SQL Server 2000, there is no way, can only find a 2005 or 2008 of the database restore, and then through the linked server into your database, do not forget the longitude, latitude must be clustered index, or simply remove the ID, direct longitude and latitude together as the primary key.
I would like to express my sincere apologies for the way that this method may have affected some people.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.