Difference between revisions of "Land usage"

From Geohashing
imported>Superbest
(Usage)
imported>Superbest
(Usage)
Line 6: Line 6:
 
# Save it in your preferred lossless image format (png is fine).
 
# Save it in your preferred lossless image format (png is fine).
 
# Run this script with the image file as a parameter.  
 
# Run this script with the image file as a parameter.  
Note: You may have to adapt the color values - read the next section.
+
Note: You may have to adapt the color values - read the next section. This may be the problem if you are getting no output.
  
 
Note on versions: As of 17 June, 2009, the module [python-image http://www.pythonware.com/products/pil/] which is required by this script only works with python 2.6.
 
Note on versions: As of 17 June, 2009, the module [python-image http://www.pythonware.com/products/pil/] which is required by this script only works with python 2.6.

Revision as of 18:06, 17 June 2009

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. This may be the problem if you are getting no output.

Note on versions: As of 17 June, 2009, the module [python-image http://www.pythonware.com/products/pil/] which is required by this script only works with python 2.6.

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. Also, due to some shading effects, several colours are used for the same type of area across the graticule. 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. I have tried to compile a first list of used colours in the source code, please update this as needed.

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".

Basically, the script currently differentiates between:

  • Everything pale white: Uncharted land - usually: agriculture, wilderness, ...
  • Everything light grey: Settled land - larger cities
  • Everything dark grey: Restricted areas - Industrial, Military, Airports, ...
  • Everything pale green: Forests
  • Everything dark green: Natural reserves, parks, and golf courses.
  • Everything blue: Water.
  • Everything yellow: Larger roads, which you can still see on the smaller scales.
  • Everything orange: Highways, motorways.

Depending on your graticule, the description you would want to use may differ. But it's usually easier to fix that after the calculation.

Example

./graticount.py berlin.png
37.03%	Forests
29.14%	Fields
11.23%	Natural reserves
9.19%	Roads
6.84%	Settlements
3.84%	Highways
2.12%	Water
0.62%	Industrial

Code

it's python. ready for take-off.

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

colors = {
          (183,205,161,255):"Natural reserves",
          (183,205,162,255):"Natural reserves",
          (184,206,162,255):"Natural reserves",
          (185,207,163,255):"Natural reserves",
          (211,215,198,255):"Forests",
          (211,215,199,255):"Forests",
          (212,215,199,255):"Forests",
          (212,216,199,255):"Forests",
          (213,217,200,255):"Forests",
          (216,208,206,255):"Industrial",
          (216,208,207,255):"Industrial",
          (216,210,210,255):"Industrial",
          (218,210,218,255):"Industrial",
          (235,224,214,255):"Settlements",
          (235,224,215,255):"Settlements",
          (235,224,216,255):"Settlements",
	  (235,225,216,255):"Settlements",
	  (236,225,216,255):"Settlements", 
          (237,226,217,255):"Settlements",
          (242,195, 72,255):"Highways",
          (243,195, 72,255):"Highways",
          (243,196, 72,255):"Highways",
          (243,196, 73,255):"Highways",
          (243,197, 71,255):"Highways",
          (244,196, 73,255):"Highways",
          (245,197,73 ,255):"Highways",
          (242,233,227,255):"Fields", 
          (243,233,228,255):"Fields",
          (243,233,229,255):"Fields",
          (243,234,229,255):"Fields",
	  (243,235,229,255):"Fields",
          (245,235,230,255):"Fields",
          (252,241,134,255):"Roads",
          (252,241,135,255):"Roads",
          (252,242,135,255):"Roads",
          (253,242,135,255):"Roads",
          (253,243,135,255):"Roads",
          (254,243,135,255):"Roads",
	  (254,244,134,255):"Roads",
          (255,244,136,255):"Roads",
          (171,185,205,255):"Water",
	  (171,186,206,255):"Water",
          (172,185,205,255):"Water",
          (172,186,205,255):"Water",
          (172,186,206,255):"Water",
          (173,187,207,255):"Water",
	  (254,132, 93,255):"Intercity Highways",
         }

stats = {}
counts = {}
total = 0
results = []

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():
  results.append((count*100.0/total, label))
results.sort(reverse=True)
for result in results:
  print("%.2f%%\t%s" % result)