Generating WSG84 (latitude longitude based) Spatial Polygons from Geographic Map Images - keyurfaldu/spatial GitHub Wiki

written by Keyur Faldu, Data Scientist, Bangalore

This is a general election year in India, 2014. We thought to visualise historical results, and current trends for all constituencies. We hit a roadblock, as spatial data for indian constituencies were not available. Few data provider charges it heavily, more than USD 1K. For an amateur project, I thought to reverse engineer lat-long based spatial polygon data (WSG84) for 543 Indian parliamentary constituencies.

Finally, I could generate Indian parliamentary constituencies data, and hence could do various kinds of visualization. Spatial data can be downloaded from, India Parliamentary Constituencies. For an instance, General election 2009 can be visualized with this spatial data as, General Election, India, 2009

It is just a 7 step process to converting geographic map to lat-long based spatial polygon data.

  1. Procure: procure map images (plenty of them are generally available on web) which you want to convert to spatial polygon data. We could get separate map file for each of 28 Indian states.

  2. Mask: mask map images to generate black & white boundary images. Masked Image

  3. Generate: generate boundary data in image based x-y co-ordinates by boundary tracing algorithm. Boundary Tracing

  4. Superimpose: superimpose few x-y co-ordinates to lat-long ordinates with the help maps.google.com. This is a manual process. Lat-long co-ordinates (WSG84) are spherical, convert them into mercator co-ordinates (EPSG:3857).

    [SUPERIMPOSE]
    P1 = [(138, 1074), (76.838379,28.57608)]
    P2 = [(1605, 957), (77.338042,28.608637)]
    P3 = [(765, 99), (77.048278,28.873539)]
    P4 = [(285, 1326), (76.879363,28.507316)]
    
  5. Regress: regress all the x-y co-ordinates on map boundary to mercator co-ordinates based on superimposed points based weighted extrapolation.

  6. Transform: transform mercator co-ordinates boundary polygon to lat-long co-ordinates. Hence, we got spatial polygon data (WSG84). So, we can play with all kinds of visualization on this spatial image.

    Spatial Image

  7. Jigsaw: append all spatial polygon data, thus, various geographic regions from different map images should fit together well, as polygon data is based on lat-long co-ordinates. As can be seen above, we could generate India map by fitting together spatial data for 28 different states.

With this 7-step process, it is fairly straight forward to generate spatial polygon data of any raw geometric map file. Check out code repository for python implementation of each of these above steps.

How to execute:

  1. Write a config file to specify all input data, i.e. masked map image, superimposed points, filepaths. sample config file

  2. Run following command:

    python SpatialDataGenerator.py -c <config_file>