15

Does anyone know of a way to batch convert shapefiles to jpegs or another common image format?

edit: I should have clarified that I would like to apply a styling to each shapefile, and then export the shapefile with that styling to an image.

For example, I have census tracts in a county, and I want an image of each individual tract highlighted while the other tracts are all the same color.

4
  • Do you have any particular software in mind? An application that you use regularly? Or do you just any way possible? Jun 3, 2013 at 20:47
  • I use Arc and Q pretty regularly and am getting more familiar with arcpy, but am really just looking for any way to do this, even another third party software package
    – camdenl
    Jun 3, 2013 at 21:49
  • 2
    So for clarification, you are basically just looking to generate thumbnails (or larger) of the shapefile, no styling, no colors, no labels, etc right? Jun 3, 2013 at 22:21
  • I started thinking about this last night, and I see that my original question might be misleading. I would like to apply a styling and then export the map over 100+ times for each indiviual shapefile with the applied styling.
    – camdenl
    Jun 4, 2013 at 13:19

6 Answers 6

14

There are many free tools like:

But in Python, the reference is Mapnik and Getting Started In Python

  • You create a map (width and height in pixels, background color, etc)
  • You create Styles which determines how the data is rendered
  • You add a datasource (shapefile, etc.) and create a layer from it.
  • You render the map
  • the same map and styles parameters can be applied to multiple files (via an XML file)

see the examples in Mapniks Maps

Simple Rendered images from TM_WORLD_BORDERS-0.3.shp

enter image description here

selection of a country (Angola) in the shapefile:

enter image description here

Another example from Mapniks Maps

enter image description here

4

You could add all of the layers to a mxd, then loop through them and run

arcpy.mapping.ExportToJPEG(map_document, out_jpeg, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {jpeg_quality}, {progressive})

for each layer in the map.

1
3

This similar question has answers that use FME to convert DWG to JPG. The process will be similar for converting shapefiles.

This example has example workspaces you can download.

FME is well suited to handling batch processes. For example, you can point the reader to a folder and it will include all shapefiles in the folder.

I created a simple workflow which reads all the shapefiles in a folder and writes them out to separate JPGs.

workflow

3

Oh, I did this yesterday with counties in Montana! Is it too late to answer? Assuming you've already used Split to make a shapefile for each census tract, I found that it was easy (lazy) to handle them in a Group Layer. Assuming that's the only Group Layer in your document, don't be shy about opening up the ArcPy window and entering:

# Setup, defining a variable for the map document, the data frame,
# and a list of layers:
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd)

# To copy symbology to all subLayers in the group layer,
# using a template, any normal polygon shapefile will do:
# (datum conflict warnings are irrelevant)
for layer in layers:
  if layer.isGroupLayer:
    for subLayer in layer:
      arcpy.ApplySymbologyFromLayer_management(subLayer, "templatelayer")

# Export one map with each county/tract highlighted, toggling visibility
# of each sublayer before and after:
for layer in layers:
  if layer.isGroupLayer:
    for subLayer in layer:
      print "Exporting " + str(subLayer.name)
      subLayer.visible = True
      slfile = "C:\\YourPathHere\\Subdir\\Etc\\" + str(subLayer.name) +
".png"
      arcpy.mapping.ExportToPNG(mxd, slfile, df, df_export_width=640,
df_export_height=480, transparent_color="255, 255, 255")
      subLayer.visible = False

Export to jpg is similar, but jpgs are kinda yucky. This was my first ArcPy experience, so I'm sure there are more elegant ways to do this.

1
  • You did do the exact same thing eh? Glad you figured it out, thanks for the response
    – camdenl
    Jun 25, 2013 at 1:37
2

Here's a Python script I use. It can modified to change the colour of the polygons, &c:

#!/usr/bin/env python3
from descartes import PolygonPatch
import matplotlib.pyplot as plt 
import random
import shapefile
import sys

def get_cmap(n, name='hsv'):
  '''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct 
  RGB color; the keyword argument name must be a standard mpl colormap name.'''
  return plt.cm.get_cmap(name, n)

if len(sys.argv)!=3:
  print("Syntax: {0} <Shapefile> <Output>".format(sys.argv[0]))
  sys.exit(-1)

shapefile_name = sys.argv[1]
outputfile     = sys.argv[2]

polys  = shapefile.Reader(shapefile_name)
shapes = polys.shapes()
cmap   = get_cmap(len(shapes))

#Enable to randomize colours
#random.shuffle(shapes)

fig = plt.figure()
ax  = fig.add_axes((0,0,1,1)) 

for i,poly in enumerate(shapes):
  poly  = poly.__geo_interface__
  color = cmap(i)
  ax.add_patch(PolygonPatch(poly, fc=None, ec="black", alpha=1, zorder=2))

ax.axis('scaled')
ax.set_axis_off()
plt.savefig(outputfile, bbox_inches='tight', pad_inches=0)
-1

Instead, just load the shp file into ArcMaps and configure it how you like. Then use either Alt + Print Screen or the Snipping Tool to get a screenshot. Then you will have a jpg or png that looks exactly like how the shape file was rendered.

1
  • 3
    I don't think that addresses the key component of the question which is how to automate the process. Nov 25, 2013 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.