Search code examples
imageopen-sourcegisesrimapinfo

How do I convert ESRI or MapInfo GIS data to images using free tools?


The Australian Electoral Commission has free ESRI and MapInfo formate GIS layers of Australian electoral boundaries for download. I want to convert this data to a thumbnail polygon image using a free tool.


Solution

  • I'm assuming you want a separate image for each electorate? If so, I would take the following approach using python:

    Read the geometry using GDAL/OGR:

    Install the GDAL/OGR tools and their python bindings. Download the ESRI shapefile for the electoral boundaries. Ensure you can read the polygon geometry using OGR:

    import sys
    import ogr
    
    ds = ogr.Open( "/path/to/boundary/file.shp" )
    if ds is None:
        print "Open failed.\n"
        sys.exit( 1 )
    
    lyr = ds.GetLayer(0)
    
    lyr.ResetReading()
    
    feat = lyr.GetNextFeature()
    while feat is not None:
        geom = feat.GetGeometryRef()
        if geom is None or geom.GetGeometryType() != ogr.wkbPolygon:
            print "no poly geometry\n"
    
        feat = lyr.GetNextFeature()
    
    ds.Destroy()
    

    Output the geometry using matplotlib via shapely, descartes

    Install matplotlib, shapely and descartes. Modify the above script to load each polygon into matplob via shapely and descartes:

    import sys
    import ogr
    from shapely.wkb import loads
    from descartes import PolygonPatch
    from matplotlib import pyplot
    
    
    ds = ogr.Open( "/path/to/boundary/file.shp" )
    if ds is None:
        print "Open failed.\n"
        sys.exit( 1 )
    
    lyr = ds.GetLayer(0)
    
    lyr.ResetReading()
    
    feat = lyr.GetNextFeature()
    while feat is not None:
        geom = feat.GetGeometryRef()
        if geom is None or geom.GetGeometryType() != ogr.wkbPolygon:
            print "no poly geometry\n"
        else:
          # create matplotlib figure:
          fig = pyplot.figure(1, figsize = [10,10], dpi = 300)   #create 10x10 figure
          ax = fig.addsubplot(111)    #Add the map frame (single plot)
    
          # add polygon:
          patch = PolygonPatch(loads(feature.GetGeometryRef().ExportToWkb()), plus colour and line considerations)
          ax.addpatch(patch)   # simply add the patch to the subplot
    
          # set plot vars
          ax.set_xlim(get xmin and xmax values from data)
          ax.set_ylim(get ymin and ymax values from data)
          ax.set_aspect(1)
    
          # save as image
          pyplot.savefig('somefile.png', some arguments you like)¶
    
        feat = lyr.GetNextFeature()
    
    ds.Destroy()
    

    Obviously you need to fix this up a bit to get it to draw how you want, but the general approach should be sound.