Land usage

From Geohashing
Revision as of 22:11, 19 August 2008 by imported>Relet (+now allows for multiple colours to have the same meaning)

Here's a small piece of code that allows you to calculate the land usage distribution in your graticule. It's a hack, and you should know how to interpret the results.

Usage

  1. Highlight your graticule in the peeron map.
  2. Make a screenshot (moar zoom = moar better).
  3. Save it in your preferred lossless image format (png is fine).
  4. Run this script with the image file as a parameter.

Note: You may have to adapt the color values - read the next section.

How it works

The script basically counts pixels on your screenshot. It has a list of colours which are used on the map for certain areas.

On the screenshot of your graticule, all pixels within the graticule highlight are slightly rosé, compared to everything else. Forests are green-rosé, Natural reserves are dark-green-rosé, bodies of water are blue-rosé, and so on. The script counts all pixels for which a meaning is given, and ignores everything else. Finally, it compares the count of each colour with the total count of identified pixels.

Note that Google uses antialiasing. Hence, the script will only recognize large areas of uniform colour, but not anything in-between. The colours given are some few examples for the rendering used in Germany - if for example your highways are rendered in a different colour, you may have to adapt it. You may also want to add your own. To do so, use the pipette tool in your favourite image editor and select a pixel in a large field of uniform colour. Copy the colour values for R(ed), G(reen), B(lue) - and A(lpha = opacity), which should always be 255.

As the maps overemphasize roads on smaller scales, their results will likewise be exaggerated. You may want to ignore them alltogether, or interpret the value as "being rather close to a road/highway".

Example

./graticount.py berlin.png
3.48%   Highways
35.70%  Fields
6.76%   Settlements
33.58%  Forests
10.24%  Natural reserves
1.91%   Water
8.32%   Roads

Code

it's python. ready for take-off.

#!/usr/bin/env python
import Image, sys

colors = {(185,207,163,255):"Natural reserves",
          (213,217,200,255):"Forests",
#         (211,215,198,255):"Forests", #example: multiple colours are supported for the same land type 
          (245,235,230,255):"Fields",
          (255,244,136,255):"Roads",
          (245,197,73 ,255):"Highways",
          (173,187,207,255):"Water",
          (237,226,217,255):"Settlements",
          (218,210,218,255):"Restricted",
         }

stats = {}
counts = {}
total = 0

image = Image.open(sys.argv[1])
for pixel in image.getdata():
  stats[pixel] = (pixel in stats) and stats[pixel]+1 or 1

for pixel, count in stats.iteritems():
  if pixel in colors:
    counts[colors[pixel]] = colors[pixel] in counts and counts[colors[pixel]]+count or count

for label, count in counts.iteritems():
  total = total + count
for label, count in counts.iteritems():
  print "%.2f%%\t%s" % (count*100.0/total, label)