Admin Levels at Different Zoom Levels

Few days back, I got a task to show admin_levels at various zoom levels. I here explain the task in detail.

It means like to show all states (admin_level=4) of India at particular zoom level with a different colour. Likewise to show all districts (having a particular admin_level) with another colour. It was quite interesting task.

To know more about admin_levels you can browse to the link.

http://wiki.openstreetmap.org/wiki/Tag:boundary%3Dadministrative

The algorithm behind it is to give the reference to particular admin level. For example column “admin_level_2” will refer to all the rows where admin_level=2.

For this you must know basics of Postgresql.

– id: “zoom_levels”
name: “zoom_levels”
class: “”
geometry: “polygon”
<<: *extents
Datasource:
<<: *osm2pgsql
table: |-
(SELECT boundary,
COALESCE(tags->’name:pa’,tags->’name:hi’,name) as name, way, COALESCE(
‘admin_level_’ || CASE WHEN admin_level IN (‘2’, ‘3’,’4′,’5′,’6′,’7′,’8′,’9′,’10’) THEN admin_level ELSE NULL END
) as admin_level, access,
religion,
way_area/NULLIF(!pixel_width!::real*!pixel_height!::real,0) AS way_pixels
FROM planet_osm_polygon
WHERE admin_level IN (‘2’, ‘3’,’4′,’5′,’6′,’7′,’8′,’9′,’10’)
) AS zoom_levels
properties:
minzoom: 3

The above is the complete layer with id “zoom_levels”. The styles to this layer will be given through this ID.

Now create fresh mss file called zoom-levels.mss

#zoom_levels {
[admin_level = ‘admin_level_2’][zoom>=3][zoom<5] {
text-name: “[name]”;
text-line-spacing: -1.3;
text-size:12;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;
line-color: #000;
polygon-fill: #765543;
polygon-gamma:0.4;
polygon-opacity: 0.5;}
[admin_level = ‘admin_level_3’][zoom>=5][zoom<6] {
polygon-fill: @landform-color * 0.1;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:12;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_4’][zoom>=6][zoom<8]{
polygon-fill: #cccccc;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:16;
text-line-spacing: -1.65;
text-halo-radius: @standard-halo-radius;

text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_5’][zoom>=8][zoom<10] {
polygon-fill: #ccc149;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:11;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_6’][zoom>=10][zoom<12] {
polygon-fill: #cc1039;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:14;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_7’][zoom>=12][zoom<14] {
polygon-fill: #1e7b79;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:13;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_8’][zoom>=14][zoom<16] {
polygon-fill: #6c7b6d;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:13;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_9’][zoom>=16][zoom<17] {
polygon-fill: #d68035;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;
text-size:13;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_10’][zoom>=17][zoom<19] {
polygon-fill: #295332;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;

text-size:12;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts

}
}

Include this mss file in the project.mml file as other mss files are included.

Now it’s time to compile the code.:)

carto project.mml > style.xml

Re-render the tiles. You may see the desired output at admin_level=4. It’s obvious that you should have India database.

admin.png

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s