### Coordinate Transformation Examples in Rust Source: https://github.com/athxx/coordtransform-rs/blob/main/README.md Demonstrates various coordinate system conversions including BD09 to GCJ02, GCJ02 to BD09, WGS84 to GCJ02, GCJ02 to WGS84, BD09 to WGS84, WGS84 to BD09, WGS84 to EPSG:3857, EPSG:3857 to WGS84, GCJ02 to EPSG:3857, and BD09 to EPSG:3857. ```rust use coordtransform::* fn main() { // 百度坐标系 -> 火星坐标系 let (lon, lat) = bd09_to_gcj02(116.404, 39.915); println!("BD09 to GCJ02: ({}, {})", lon, lat); // 火星坐标系 -> 百度坐标系 let (lon, lat) = gcj02_to_bd09(116.404, 39.915); println!("GCJ02 to BD09: ({}, {})", lon, lat); // WGS84坐标系 -> 火星坐标系 let (lon, lat) = wgs84_to_gcj02(116.404, 39.915); println!("WGS84 to GCJ02: ({}, {})", lon, lat); // 火星坐标系 -> WGS84坐标系 gcj02 -> WGS84 let (lon, lat) = gcj02_to_wgs84(116.404, 39.915); println!("GCJ02 to WGS84: ({}, {})", lon, lat); // 百度坐标系 -> WGS84坐标系 let (lon, lat) = bd09_to_wgs84(116.404, 39.915); println!("BD09 to WGS84: ({}, {})", lon, lat); // WGS84坐标系 -> 百度坐标系 let (lon, lat) = wgs84_to_bd09(116.404, 39.915); println!("WGS84 to BD09: ({}, {})", lon, lat); // WGS84坐标系 -> EPSG:3857坐标系 (Web墨卡托投影) let (x, y) = wgs84_to_epsg3857(116.404, 39.915); println!("WGS84 to EPSG:3857: ({}, {})", x, y); // EPSG:3857坐标系 -> WGS84坐标系 let (lon, lat) = epsg3857_to_wgs84(12958752.0, 4825923.0); println!("EPSG:3857 to WGS84: ({}, {})", lon, lat); // GCJ02坐标系 -> EPSG:3857坐标系 let (x, y) = gcj02_to_epsg3857(116.404, 39.915); println!("GCJ02 to EPSG:3857: ({}, {})", x, y); // BD09坐标系 -> EPSG:3857坐标系 let (x, y) = bd09_to_epsg3857(116.404, 39.915); println!("BD09 to EPSG:3857: ({}, {})", x, y); } ``` -------------------------------- ### Complete Coordinate Conversion Workflow Source: https://context7.com/athxx/coordtransform-rs/llms.txt Use this example to demonstrate a full coordinate conversion workflow, including conversion to Amap/Google China (GCJ02), Baidu Maps (BD09), and Web Tiles (EPSG:3857). It also shows a precision test for round-trip conversion from EPSG:3857 back to WGS84. Ensure the 'coordtransform' crate is added as a dependency. ```rust use coordtransform::* fn main() { // Scenario: You receive GPS coordinates from a device and need to display // them on multiple Chinese map services let gps_lon = 116.404; // Raw GPS longitude (WGS84) let gps_lat = 39.915; // Raw GPS latitude (WGS84) println!("=== Multi-Platform Coordinate Conversion ===\n"); println!("Original GPS (WGS84): ({}, {})\n", gps_lon, gps_lat); // For Google Maps China / Amap (GCJ02) let (amap_lon, amap_lat) = wgs84_to_gcj02(gps_lon, gps_lat); println!("For Amap/Google China (GCJ02): ({:.6}, {:.6})", amap_lon, amap_lat); // For Baidu Maps (BD09) let (baidu_lon, baidu_lat) = wgs84_to_bd09(gps_lon, gps_lat); println!("For Baidu Maps (BD09): ({:.6}, {:.6})", baidu_lon, baidu_lat); // For Web tiles / OpenStreetMap (EPSG:3857) let (web_x, web_y) = wgs84_to_epsg3857(gps_lon, gps_lat); println!("For Web Tiles (EPSG:3857): ({:.2}, {:.2})\n", web_x, web_y); // Round-trip precision test println!("=== Precision Test (Round-Trip) ===\n"); let (back_lon, back_lat) = epsg3857_to_wgs84(web_x, web_y); println!("Original: ({:.10}, {:.10})", gps_lon, gps_lat); println!("After round-trip: ({:.10}, {:.10})", back_lon, back_lat); println!("Difference: ({:.2e}, {:.2e})", (back_lon - gps_lon).abs(), (back_lat - gps_lat).abs()); } ``` -------------------------------- ### Run Benchmark Tests Source: https://github.com/athxx/coordtransform-rs/blob/main/README.md Execute the benchmark tests for the coordinate transformation library using Cargo. ```bash cargo bench ``` -------------------------------- ### Run Tests Source: https://github.com/athxx/coordtransform-rs/blob/main/README.md Execute the unit tests for the coordinate transformation library using Cargo. ```bash cargo test ``` -------------------------------- ### Add coordtransform to Cargo.toml Source: https://github.com/athxx/coordtransform-rs/blob/main/README.md To use the library, add the `coordtransform` crate to your project's `Cargo.toml` file. ```toml [dependencies] coordtransform = "0.3.0" ``` -------------------------------- ### Convert EPSG:3857 to BD09 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Web Mercator projection coordinates to BD09 Baidu coordinates. ```rust use coordtransform::epsg3857_to_bd09; fn main() { // Web Mercator coordinates let x = 12958752.67; let y = 4825923.04; // Convert to BD09 for display on Baidu Maps let (lon, lat) = epsg3857_to_bd09(x, y); println!("EPSG:3857: ({}, {})", x, y); println!("BD09: ({:.6}, {:.6})", lon, lat); } ``` -------------------------------- ### Convert BD09 to EPSG:3857 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts BD09 Baidu coordinates to Web Mercator projection. ```rust use coordtransform::bd09_to_epsg3857; fn main() { // BD09 coordinates (from Baidu Maps) let bd09_lon = 116.404; let bd09_lat = 39.915; // Convert to Web Mercator let (x, y) = bd09_to_epsg3857(bd09_lon, bd09_lat); println!("BD09: ({}, {})", bd09_lon, bd09_lat); println!("EPSG:3857: ({:.2}, {:.2})", x, y); // Output: // BD09: (116.404, 39.915) // EPSG:3857: (12956641.65, 4851095.83) } ``` -------------------------------- ### Convert EPSG:3857 to WGS84 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Web Mercator projection coordinates back to WGS84 geographic coordinates. ```rust use coordtransform::epsg3857_to_wgs84; fn main() { // Web Mercator coordinates in meters let x = 12958034.01; let y = 4853597.99; // Convert to geographic coordinates let (lon, lat) = epsg3857_to_wgs84(x, y); println!("EPSG:3857: ({}, {})", x, y); println!("WGS84: ({:.6}, {:.6})", lon, lat); // Output: // EPSG:3857: (12958034.01, 4853597.99) // WGS84: (116.404000, 39.915000) } ``` -------------------------------- ### Convert EPSG:3857 to GCJ02 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Web Mercator projection coordinates to GCJ02 Mars coordinates. ```rust use coordtransform::epsg3857_to_gcj02; fn main() { // Web Mercator coordinates let x = 12958752.67; let y = 4825923.04; // Convert to GCJ02 for display on Amap let (lon, lat) = epsg3857_to_gcj02(x, y); println!("EPSG:3857: ({}, {})", x, y); println!("GCJ02: ({:.6}, {:.6})", lon, lat); } ``` -------------------------------- ### Convert Mars Coordinates (GCJ02) to Baidu Coordinates (BD09) Source: https://context7.com/athxx/coordtransform-rs/llms.txt Use this function to convert coordinates from the Mars system to Baidu's system, useful for displaying Google Maps or Amap data on Baidu Maps. ```rust use coordtransform::gcj02_to_bd09; fn main() { // Coordinates in GCJ02 (Google Maps China / Amap) let gcj02_lon = 116.404; let gcj02_lat = 39.915; // Convert to BD09 (Baidu) let (bd09_lon, bd09_lat) = gcj02_to_bd09(gcj02_lon, gcj02_lat); println!("GCJ02: ({}, {})", gcj02_lon, gcj02_lat); println!("BD09: ({:.10}, {:.10})", bd09_lon, bd09_lat); // Output: // GCJ02: (116.404, 39.915) // BD09: (116.4103694937, 39.9213369935) } ``` -------------------------------- ### Convert WGS84 to EPSG:3857 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts WGS84 geographic coordinates to Web Mercator projection coordinates used in web mapping. ```rust use coordtransform::wgs84_to_epsg3857; fn main() { // Geographic coordinates in degrees let wgs84_lon = 116.404; // Beijing longitude let wgs84_lat = 39.915; // Beijing latitude // Convert to Web Mercator (meters) let (x, y) = wgs84_to_epsg3857(wgs84_lon, wgs84_lat); println!("WGS84: ({}, {})", wgs84_lon, wgs84_lat); println!("EPSG:3857: ({:.2}, {:.2})", x, y); // Output: // WGS84: (116.404, 39.915) // EPSG:3857: (12958034.01, 4853597.99) // Test with multiple cities let cities = [ ("Shanghai", 121.4737, 31.2304), ("New York", -74.0060, 40.7128), ("London", 0.1276, 51.5074), ]; for (city, lon, lat) in cities { let (x, y) = wgs84_to_epsg3857(lon, lat); println!("{}: ({:.2}, {:.2})", city, x, y); } } ``` -------------------------------- ### Convert GCJ02 to EPSG:3857 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts GCJ02 Mars coordinates to Web Mercator projection by first converting to WGS84. ```rust use coordtransform::gcj02_to_epsg3857; fn main() { // GCJ02 coordinates (from Amap) let gcj02_lon = 116.404; let gcj02_lat = 39.915; // Convert to Web Mercator for tile calculations let (x, y) = gcj02_to_epsg3857(gcj02_lon, gcj02_lat); println!("GCJ02: ({}, {})", gcj02_lon, gcj02_lat); println!("EPSG:3857: ({:.2}, {:.2})", x, y); // Output: // GCJ02: (116.404, 39.915) // EPSG:3857: (12957352.67, 4852064.57) } ``` -------------------------------- ### Convert Baidu Coordinates (BD09) to Mars Coordinates (GCJ02) Source: https://context7.com/athxx/coordtransform-rs/llms.txt Use this function to convert coordinates from Baidu's system to the Mars system, useful for displaying Baidu data on Google Maps China or Amap. ```rust use coordtransform::bd09_to_gcj02; fn main() { // Beijing coordinates in BD09 (Baidu) let bd09_lon = 116.404; let bd09_lat = 39.915; // Convert to GCJ02 (Mars coordinates) let (gcj02_lon, gcj02_lat) = bd09_to_gcj02(bd09_lon, bd09_lat); println!("BD09: ({}, {})", bd09_lon, bd09_lat); println!("GCJ02: ({:.10}, {:.10})", gcj02_lon, gcj02_lat); // Output: // BD09: (116.404, 39.915) // GCJ02: (116.3976272912, 39.9086567396) } ``` -------------------------------- ### bd09_to_gcj02 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts coordinates from Baidu Coordinate System (BD09) to Mars Coordinate System (GCJ02). ```APIDOC ## FUNCTION bd09_to_gcj02 ### Description Converts coordinates from Baidu Coordinate System (BD09) to Mars Coordinate System (GCJ02). ### Parameters - **bd09_lon** (f64) - Required - Longitude in BD09 format - **bd09_lat** (f64) - Required - Latitude in BD09 format ### Response - **(f64, f64)** - Returns a tuple containing the converted (longitude, latitude) in GCJ02 format. ``` -------------------------------- ### gcj02_to_bd09 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts coordinates from Mars Coordinate System (GCJ02) to Baidu Coordinate System (BD09). ```APIDOC ## FUNCTION gcj02_to_bd09 ### Description Converts coordinates from Mars Coordinate System (GCJ02) to Baidu Coordinate System (BD09). ### Parameters - **gcj02_lon** (f64) - Required - Longitude in GCJ02 format - **gcj02_lat** (f64) - Required - Latitude in GCJ02 format ### Response - **(f64, f64)** - Returns a tuple containing the converted (longitude, latitude) in BD09 format. ``` -------------------------------- ### Convert WGS84 to BD09 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Performs a compound conversion from WGS84 GPS coordinates to Baidu BD09 coordinates. ```rust use coordtransform::wgs84_to_bd09; fn main() { // Raw GPS coordinates (WGS84) let wgs84_lon = 116.404; let wgs84_lat = 39.915; // Convert directly to BD09 for Baidu Maps let (bd09_lon, bd09_lat) = wgs84_to_bd09(wgs84_lon, wgs84_lat); println!("WGS84 (GPS): ({}, {})", wgs84_lon, wgs84_lat); println!("BD09 (Baidu): ({:.10}, {:.10})", bd09_lon, bd09_lat); // Output: // WGS84 (GPS): (116.404, 39.915) // BD09 (Baidu): (116.4166272438, 39.9226995522) } ``` -------------------------------- ### Convert Baidu Coordinates (BD09) to WGS84 Coordinates Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Baidu coordinates (BD09) directly to standard GPS coordinates (WGS84). This is a compound conversion that first converts BD09 to GCJ02, then GCJ02 to WGS84. ```rust use coordtransform::bd09_to_wgs84; fn main() { // Coordinates from Baidu Maps (BD09) let bd09_lon = 116.404; let bd09_lat = 39.915; // Convert directly to WGS84 (standard GPS) let (wgs84_lon, wgs84_lat) = bd09_to_wgs84(bd09_lon, bd09_lat); println!("BD09 (Baidu): ({}, {})", bd09_lon, bd09_lat); println!("WGS84 (GPS): ({:.10}, {:.10})", wgs84_lon, wgs84_lat); // Output: // BD09 (Baidu): (116.404, 39.915) // WGS84 (GPS): (116.3913836995, 39.9072532145) } ``` -------------------------------- ### bd09_to_wgs84 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Baidu coordinates (BD09) directly to standard GPS coordinates (WGS84). ```APIDOC ## FUNCTION bd09_to_wgs84 ### Description Converts Baidu coordinates (BD09) directly to standard GPS coordinates (WGS84) via a compound conversion. ### Parameters - **bd09_lon** (f64) - Required - Longitude in BD09 format - **bd09_lat** (f64) - Required - Latitude in BD09 format ### Response - **(f64, f64)** - Returns a tuple containing the converted (longitude, latitude) in WGS84 format. ``` -------------------------------- ### Convert Mars Coordinates (GCJ02) to WGS84 Coordinates Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts coordinates from the Mars Coordinate System (GCJ02) back to standard GPS coordinates (WGS84). This is useful for storing coordinates in a standard format or for use with international mapping services. ```rust use coordtransform::gcj02_to_wgs84; fn main() { // Coordinates from Amap (GCJ02) let gcj02_lon = 116.404; let gcj02_lat = 39.915; // Convert to WGS84 (standard GPS) let (wgs84_lon, wgs84_lat) = gcj02_to_wgs84(gcj02_lon, gcj02_lat); println!("GCJ02: ({}, {})", gcj02_lon, gcj02_lat); println!("WGS84: ({:.10}, {:.10})", wgs84_lon, wgs84_lat); // Output: // GCJ02: (116.404, 39.915) // WGS84: (116.3977555008, 39.9135957185) } ``` -------------------------------- ### Convert WGS84 Coordinates to Mars Coordinates (GCJ02) Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts standard GPS coordinates (WGS84) to the Mars Coordinate System (GCJ02). This is essential for displaying raw GPS data on Chinese mapping services like Google Maps China or Amap. Coordinates outside China remain unchanged. ```rust use coordtransform::wgs84_to_gcj02; fn main() { // Raw GPS coordinates (WGS84) let wgs84_lon = 116.404; let wgs84_lat = 39.915; // Convert to GCJ02 for display on Amap let (gcj02_lon, gcj02_lat) = wgs84_to_gcj02(wgs84_lon, wgs84_lat); println!("WGS84 (GPS): ({}, {})", wgs84_lon, wgs84_lat); println!("GCJ02 (Amap): ({:.10}, {:.10})", gcj02_lon, gcj02_lat); // Output: // WGS84 (GPS): (116.404, 39.915) // GCJ02 (Amap): (116.4102444992, 39.9164042815) // Coordinates outside China remain unchanged let (lon, lat) = wgs84_to_gcj02(0.0, 0.0); println!("Outside China: ({}, {})", lon, lat); // Output: Outside China: (0, 0) } ``` -------------------------------- ### wgs84_to_gcj02 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts GPS coordinates (WGS84) to Mars Coordinate System (GCJ02). ```APIDOC ## FUNCTION wgs84_to_gcj02 ### Description Converts GPS coordinates (WGS84) to Mars Coordinate System (GCJ02). Coordinates outside China remain unchanged. ### Parameters - **wgs84_lon** (f64) - Required - Longitude in WGS84 format - **wgs84_lat** (f64) - Required - Latitude in WGS84 format ### Response - **(f64, f64)** - Returns a tuple containing the converted (longitude, latitude) in GCJ02 format. ``` -------------------------------- ### gcj02_to_wgs84 Source: https://context7.com/athxx/coordtransform-rs/llms.txt Converts Mars Coordinate System (GCJ02) back to standard GPS coordinates (WGS84). ```APIDOC ## FUNCTION gcj02_to_wgs84 ### Description Converts Mars Coordinate System (GCJ02) back to standard GPS coordinates (WGS84). ### Parameters - **gcj02_lon** (f64) - Required - Longitude in GCJ02 format - **gcj02_lat** (f64) - Required - Latitude in GCJ02 format ### Response - **(f64, f64)** - Returns a tuple containing the converted (longitude, latitude) in WGS84 format. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.