Sunday, 8 July 2018

open source gis - Creating simple image from vector data


Can anyone suggest a quick (and preferably open source) way to create an image from a shapefile. For example, I have a shapefile containing parcel boundaries, I want to create a png file depicting these parcels. The symbology required for this image will be bare-bones (solid outline and fill).


I know that shp2img from MapServer would do the trick. I am just curious if there is anything else available.


Any references to pre-compiled tools or api's/sdks welcome.



Answer




You can use python (modules: shapley, GDAL/OGR, numpy, matplotlib) and GDAL/OGR to draw image from almost any vector data souce, in you case shapefile. Maybe this will help you.


Example:


from shapely.geometry import Polygon
from shapely.wkb import loads
from osgeo import ogr
from matplotlib import pyplot

def drawPoligon(poligon,graf):
xLista,yLista = poligon.exterior.xy
graf.fill(xLista,yLista,"y")

graf.plot(xLista,yLista, "k-")

fig = pyplot.figure(figsize=(4, 4),dpi=180)
ax = fig.add_subplot(111)
file1 = ogr.Open("d:\\temp02\\datafile.shp")

layer = file1.GetLayerByName("datafile")
parcel = layer.GetNextFeature()

while parcel is not None:

geometryParcel = loads(parcel.GetGeometryRef().ExportToWkb())
drawPoligon(geometryParcel , ax)
parcel = layer.GetNextFeature()


pyplot.savefig('datafile.png')

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...