convert trac code chunks to github style

Dane Springmeyer 2011-11-21 02:35:35 -08:00
parent 6c8fd72419
commit 007716312b

@ -14,18 +14,20 @@ The simple check is to start python interpreter from a command line by typing py
and then just type:
#!python
```python
>>> import mapnik
```
and if you don't see any complaints, you're on the right track. If you do, you have to check your installation again.
* Note: If you have built mapnik in debug mode you should see the available datasources listed, including:
#!python
```python
>>> import mapnik
registered datasource : gdal
registered datasource : postgis
registered datasource : raster
registered datasource : shape
```
## Step 2
@ -34,7 +36,7 @@ The code below can be pasted into your python interpreter. Ideally paste line by
Import the mapnik python toolkit and setup the basic map parameters
#!python
```
import mapnik
# Instantiate a map object with given width, height and spatial reference system
@ -42,13 +44,14 @@ Import the mapnik python toolkit and setup the basic map parameters
# Set background colour to 'steelblue'.
# You can use 'named' colours, #rrggbb, #rgb or rgb(r%,g%,b%) format
m.background = mapnik.Color('steelblue')
```
* Note, Mapnik accepts any projection that Proj.4 handles. See http://spatialreference.org for the proj4 strings for various projections. For example http://spatialreference.org/ref/epsg/4326/mapnikpython/ will give you the exact parameters for EPSG 4326.
Create the Styles and Rules for the map symbology
#!python
```python
# Now lets create a style and add it to the Map.
s = mapnik.Style()
# A Style can have one or more rules. A rule consists of a filter, min/max scale
@ -65,10 +68,11 @@ Create the Styles and Rules for the map symbology
r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')))
r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1))
s.rules.append(r)
```
Connect the style information to your map and your data
#!python
```python
# Here we have to add our style to the Map, giving it a name.
m.append_style('My Style',s)
@ -78,29 +82,33 @@ Connect the style information to your map and your data
# A sample shapefile can be downloaded from http://mapnik-utils.googlecode.com/svn/data/world_borders.zip
lyr.datasource = mapnik.Shapefile(file='/Users/path/to/your/data/world_borders')
lyr.styles.append('My Style')
```
Finally add the layers to the map and zoom to the full extent of the data layer
#!python
```python
m.layers.append(lyr)
m.zoom_to_box(lyr.envelope())
```
Finish up by making the world map image
#!python
```python
# Write the data to a png image called world.png in the base directory of your user
mapnik.render_to_file(m,'world.png', 'png')
# Exit the python interpreter
exit()
```
Then back in your normal shell type:
#!sh
``sh
# On a mac
open world.png
# On windows
start world.png
```
Or navigate to your base directory and open world.png and the result should look like this:
[[BR]]
@ -113,13 +121,13 @@ The next logical step is to run the same code as a python script from your shell
This can be done by adding a line at the top of the script like:
#!python
```sh
#!/usr/bin/env python
```
Copy this entire text below and save as a file called world.py.
#!python
```python
#!/usr/bin/env python
import mapnik
@ -137,6 +145,8 @@ Copy this entire text below and save as a file called world.py.
m.layers.append(lyr)
m.zoom_to_box(lyr.envelope())
mapnik.render_to_file(m,'world.png', 'png')
```
* Don't forget to change the path to your world_borders shapefile.
* Note: Mapnik accepts both the absolute path to your data as well as the relative path
* Note: Same goes for the path to where you want to save your file
@ -144,13 +154,14 @@ Copy this entire text below and save as a file called world.py.
Next make the script executable. On Mac or Linux you would do this with the command:
#!sh
```sh
chmod +x world.py
```
Finally run the script with the command:
#!sh
```sh
# You must be in the same directory as you saved the script
./world.py
# Add a second optional command to open the resulting file with one keystroke
@ -158,6 +169,7 @@ Finally run the script with the command:
./world.py; open world.png
# On windows
start world.py && start world.png
```
* Run this way the script will continually write over and open the world.png map.
* Now you can easily open the script in a separate text editor and try changing the dimensions, colors, or datasource (remember to use the correct projection).