Map converter notes

mikakun edited this page Dec 29, 2012 · 38 revisions
Clone this wiki locally

Where to download public map data

Required packages for

For a gentoo linux system:

  • dev-lang/python <= 2.x

  • sci-libs/gdal USE="python geos"

    • sci-libs/geos USE="python"
  • dev-python/anyjson

  • Manual install: Shapely

       $ tar xf Shapely-*.tar.gz && cd Shapely-*
       $ python2 build
       # python2 install

For Debian/Ubuntu:

     # apt-get install python-anyjson python-gdal python-shapely

General usage of

  •, which includes a conversion example
  • Program synopsis is [option]... <input_file> <output_file> , also run --help for details
  • The default values of all command line options can be looked up in the source code, e.g. parser.add_argument('--name', type=str, default='world') arguments

  • input_file
    The input file, usually a *.shp.
  • output_file
    The resulting jVectorMap file (SVG-path as JSON) *.js.
  • --scale (float)
    Note: This option seems to be unused in the converter script! See option --width.
  • --country_code_index (int, 0)
    Field (index is zero-based) of layer-feature that will be used as code to uniquely identify a region in jVectorMap. You can determine the index with tools like ogrinfo. Beware that the field must be unique for all features, otherwise the map will have missing regions.
  • --country_name_index (int, 1)
    Field (index is zero-based) of layer-feature that will be used as label for a region in jVectorMap. You can determine the index with tools like ogrinfo. Again, beware that even this field must be unique for all features (due to implementation details of, otherwise the map will have missing regions.
  • --codes_file (str, "")
    Use country codes from a tab-separated values file (UTF-8 encoded) with a code/name mapping. The mapping is specified per line using the format <code><TAB><name>, e.g. DE Germany. Beware that for each feature there must be a matching code/name mapping.
  • --where (str, "")
    Optional select criteria, e.g. "ISO = 'DEU'" for only selecting data of Germany. Can also be used to exclude. E.g. Exclude Antarctica: "ne_10m_adm != 'ATA'". Where ne_10m_adm is the field name and ATA is the field value.
  • --width (float)
    Specify the width affecting the scale of the resulting SVG.
  • --insets (str)
    FIXME: Yet untested, clarification from the author needed!
    Allows to specify additional insets as JSON. Each inset needs to contain an array codes, which defines the features to use.
  • --minimal_area (float)
    FIXME: No clue what this option is for!
  • --buffer_distance (float, -0.4)
    Distance between country (outline) boundaries.
  • --simplify_tolerance (float)
    To what degree the vector paths may be simplified. Greater values result in less information/details but smaller file size. If this option is not specified or zero, simplification is not being applied.
  • --viewport (str)
    Use specified rectangle for area clipping. The format is --viewport "<lng1> <lat1> <lng2> <lat2>", e.g. --viewport "9.6 48.1 11.3 51.4".
  • --longitude0 (str, "0")
    The longitude of the horizontal center of a coordinate system (also called central meridian).
    Note: The incorrectly states that the longitude can be given as string ("10W") while its type is float. There is already a pull request on this commit.
  • --projection (str, "mill")
    The map projection to use. Projection names are compatible to those of PROJ.4. Currently implemented projections are
  • merc (Mercator)
  • mill (Miller Cylindrical)
  • aea (Albers Equal Area)
  • lcc (Lambert Conformal Conic)
  • --name (str, "world")
    Name of the map, e.g. "Germany". This value will be stored in JSON for informational purpose.
  • --language (str, "en")
    Two-letter language code, e.g. "de". This value will be stored in JSON for informational purpose.
  • --input_file_encoding (str, "iso-8859-1")
    In which encoding the input data is stored, e.g. "utf-8".
  • --precision (int, 2)

Generating maps (examples)

$ python2 --width 900 --longitude0 11.5 --name world --projection mill --language en public_map_data.shp map_world_mill_en.js

Generate a map of Germany

$ python2 \
  --width 1000 \
  --country_name_index 4 \
  --country_code_index 30 \
  --longitude0 10.71 \
  --where "ISO = 'DEU'" \
  --projection aea \
  --name datag_germany \
  --language en \
  10m-admin-1-states-provinces-shp/ne_10m_admin_1_states_provinces_shp.shp \

With v2 of natural earth data, there aren't any ISO attribute, you'll find iso_a2 instead

--where "iso_a2 = 'DE'"

Acquiring map data manually

  • Shapefile format

  • Tool ogrinfo (which makes use of GDAL)

  • Support-test and list layers

      $ ogrinfo ne_10m_admin_1_states_provinces_shp.shp
      INFO: Open of `ne_10m_admin_1_states_provinces_shp.shp'
            using driver `ESRI Shapefile' successful.
      1: ne_10m_admin_1_states_provinces_shp (Polygon)
  • Get all data of layer by layer name

      $ ogrinfo -ro ne_10m_admin_1_states_provinces_shp.shp ne_10m_admin_1_states_provinces_shp
  • Limiting results with provided WHERE-Clause using argument -where <condition>. In this example, only list rows where field ISO is DEU (Germany).

      $ ogrinfo -ro -where "ISO = 'DEU'" ne_10m_admin_1_states_provinces_shp.shp ne_10m_admin_1_states_provinces_shp

OR : learn to use SAGA-GIS to make your shape file bevore converting it

it is reasonably easy to get a grip on it, it's a very nice GUI with lots of methods. On Ubuntu you can install it from software center

  • load your natural earth 10m file

  • workspace/modules/shape tools/select by attribute to select all the region you need

  • workspace/modules/shape tools/copy selection to new shape layer

  • workspace/Data/right click on your new layer -> add to map; remove natural earth layer (will free some memory)

  • workspace/Data/right click on your new layer/attributes/show remove unnecessary fields(most of them, just keep the first one -the code- and the name), update attributes as per your app requirements

  • workspace/Data/save as shp

  • use the converter on that shape file, you'll just need to set tolerance, width, language & name

      $ python2 /path/to/ \
         --width 500 \
         --simplify_tolerance 0.044 \
         --buffer_distance -0.3 \
         --projection mill \
         --name ex \
         --language en \
         /path/to/yourcustommadeshapefile.shp \

NB: tolerance is relative to the size of shape file (if shape file is the world you'r looking at value around 1000 min, if it is just a country made using this method it is around 0.02 - depending on its size & the level of details you're after