In GIS projects, it is common to meet the degree minutesecond data import projects recorded by hand-held GPS devices provided by customers. At present, mature third-party GIS platforms use EPSG:3857 (Mercator platform coordinates) in large quantities. If we meet the existing projects of degree minutesecond data import by hand-held GPS devices, there will definitely be a lot of conversion work. This chapter discloses the degree minute second and latitude and longitude conversion simple algorithm to help programmers with similar needs work like a duck to water.

First of all, the vertical line at the North and South Poles is taken as the center of the circle. The earth is divided into 360°, 180° west and 180° east of the prime meridian, and the distance of each degree is about 111 kilometers. The actual distance varies according to the different ellipsoids in the field and different latitudes. The distance between each 1° is divided into minutes (‘) and seconds (‘), starting from the horizontal line of the equator, 90° north and 90° south. To understand this truth, we took the Hall of Prayer for Good Harvests (116.512885, 39.847469) of the Temple of Heaven in Beijing as the experimental object.

Var lon = 116.512885; Var lat = 39.847469; function transformLonlatToDD(coordinate) { var d = Math.floor(coordinate); Math.floor(coordinate % 1 * 60); //116.512885 = math.floor (coordinate % 1 * 60); //0.512885 conversion component (') is rounded after 0.512885 * 60. var s = Math.floor(coordinate % 1 * 60 % 1 * 60); //0.512885 converted to seconds (" ") is actually a small value of m * 60 rounded. var ms = coordinate % 1 * 60 % 1 * 60 % 1 * 1000; //0.512885 seconds (") cannot be discarded if there is a small value after the value. return [d, m, s, ms]; } /** * The above algorithm seems to have no problem, but the value we refer to is the coordinate value of longitude (116.512885) of Qiniandian in Beijing. * Because Chinese coordinates are almost in positive longitude and latitude values, such as -28.847469 for coordinates south of the equator, * If we also use math.floor () to round down, math.floor (-28.847469), then the value is -29!! * According to the above conversion rules, -28.847469 should be -28 degrees, which would be a serious bug in the algorithm. * So we can optimize the algorithm. */ function transformLonlatToDD(coordinate) {// Use the >> bit operator to round up, if the value is -28.847469 >> 0, the output is -28, Perfect! var d = coordinate >> 0; var m = coordinate % 1 * 60 >> 0; var s = coordinate % 1 * 60 % 1 * 60 >> 0; var ms = coordinate % 1 * 60 % 1 * 60 % 1 * 1000; return [d, m, s, ms]; } /** */ function transformDDToLonlat(d, m, s); Ms = 0.0) {var lonlat = 0.0; XXXXX = 123. XXXXX, so ms / 1000, lonlat = ms / 1000; lonlat = (lonlat + s) / 60; lonlat = (lonlat + m) / 60; lonlat += d; return lonlat; } /** * In order to facilitate interpretation and facilitate reading, the degree minute second conversion latitude and longitude of the simple algorithm step by step apart * below will use the optimization of the two functions, the icing on the cake! **/ function transformLonlatToDD(c) { return [ c >> 0, c % 1 * 60 >> 0, c % 1 * 60 % 1 * 60 >> 0, c % 1 * 60 % 1 * 60 % 1 * 1000 ] } function transformDDToLonlat(d, m, s, Ms = 0.0) {return (ms / 1000 + s) / 60 + m) / 60 + d; }Copy the code

Finally, you’ve seen how latitude and longitude conversions work, and you’ve also seen the problem of accuracy. This is a problem that all development languages use on double & float.