coordtransform_py

repository·master·Indexed 22 days ago

https://github.com/wandergis/coordtransform_py

A Python module for converting coordinates between Baidu (bd-09), Mars (gcj02), and WGS84 systems. It includes the coordTransform_utils.py module for conversions, a Geocoding class for converting Chinese addresses via the Amap API, and a coord_converter.py script for batch processing coordinates in CSV files.

Tokens
1K
Snippets
4
Records
4
Agent score
29%

What's inside coordtransform_py

  1. Examples for batch CSV coordinate conversion

    master

    Use the following command patterns to convert CSV files with coord_converter.py:

    Default column names (lng, lat):

    python coord_converter.py -i test_input.csv -o test_output.csv -t b2g

    Custom column names:

    python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度

    Skip invalid rows:

    python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度 -s True
    # 不指定经纬度列名(默认为'lng', 'lat')
    $ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g
    
    # 指定经纬度列名
    $ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度
    
    # 跳过无效经纬度的行(默认不跳过)
    $ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度 -s True
  2. Geocoding Chinese addresses to Mars coordinates

    master

    You can convert Chinese addresses to Mars (gcj02) coordinates using the Geocoding class. This requires an Amap (高德地图) API KEY.

    1. Apply for an API KEY at http://lbs.amap.com/.
    2. Initialize the Geocoding object with your key and call .geocode(address).
    g = Geocoding('API_KEY')  # 这里填写你的高德Api_Key
    g.geocode('北京市朝阳区朝阳公园')
  3. Coordinate conversion functions in coordTransform_utils.py

    master

    The coordTransform_utils.py module provides functions for converting between three coordinate systems: Baidu (bd-09), Mars (gcj02), and WGS84. The module uses only Python standard libraries and has no external dependencies.

    Available conversion functions:

    • gcj02_to_bd09(lng, lat): Mars (gcj02) $\rightarrow$ Baidu (bd09)
    • bd09_to_gcj02(lng, lat): Baidu (bd09) $\rightarrow$ Mars (gcj02)
    • wgs84_to_gcj02(lng, lat): WGS84 $\rightarrow$ Mars (gcj02)
    • gcj02_to_wgs84(lng, lat): Mars (gcj02) $\rightarrow$ WGS84
    • bd09_to_wgs84(lng, lat): Baidu (bd09) $\rightarrow$ WGS84
    • wgs84_to_bd09(lng, lat): WGS84 $\rightarrow$ Baidu (bd09)
    gcj02_to_bd09(lng, lat) # 火星坐标系->百度坐标系
    bd09_to_gcj02(lng, lat) # 百度坐标系->火星坐标系
    wgs84_to_gcj02(lng, lat) # WGS84坐标系->火星坐标系
    gcj02_to_wgs84(lng, lat) # 火星坐标系->WGS84坐标系
    bd09_to_wgs84(lng, lat) # 百度坐标系->WGS84坐标系
    wgs84_to_bd09(lng, lat) # WGS84坐标系->百度坐标系
  4. Batch convert coordinates in CSV files using coord_converter.py

    master

    The coord_converter.py script allows for batch conversion of coordinates stored in CSV files via the command line.

    usage: coord_converter.py [-h] -i INPUT -o OUTPUT -t TYPE [-n LNG_COLUMN] [-a LAT_COLUMN] [-s SKIP_INVALID_ROW]
    
    Convert coordinates in csv files.
    
    optional arguments:
      -h, --help            show this help message and exit
    
    arguments:
      -i , --input          Location of input file
      -o , --output         Location of output file
      -t , --type           Convert type, must be one of: g2b, b2g, w2g, g2w, b2w,
                            w2b
      -n , --lng_column     Column name for longitude (default: lng)
      -a , --lat_column     Column name for latitude (default: lat)
      -s , --skip_invalid_row
                            Whether to skip invalid row (default: False)