Skip to content

Commit

Permalink
Get city and town names for both place points and place polygons
Browse files Browse the repository at this point in the history
This is part of gravitystorm#103, but only fixes it for place=city/town

The SQL is moderately complex and difficult to document in the .mml file so it's worth documenting here

```sql
(SELECT way,place,name,capital,population
  FROM
    (SELECT
        way,place,name,NULL AS capital,way_area, -- The polygon table doesn't have the capital information with the default style
        CASE WHEN population~E'^\\d{1,9}$' THEN population::integer ELSE NULL END AS population
          -- We need to filter out population values that might cause exceptions. This is a fairly rigerous filtering as it will remove population=1,000
      FROM planet_osm_polygon
    UNION ALL -- Join the two together
    SELECT
        way,place,name,capital,NULL AS way_area, -- Points don't have an area
        CASE WHEN population~E'^\\d{1,9}$' AND length(population)<10 THEN population::integer ELSE NULL END AS population -- as above
      FROM planet_osm_point) AS p
  WHERE place IN ('city','town') -- This condition is brought inside by the query optimizer
  ORDER BY
    CASE place WHEN 'city' THEN 0 ELSE 10 END, -- We don't actually need this at moment with the current MSS
    CASE capital WHEN 'yes' THEN 0 ELSE 10 END, -- Nor this
    way_area DESC NULLS LAST, -- Put physically larger first
    population DESC NULLS LAST -- For points (no area) put larger population first
) AS placenames_medium
```

The ordering merits further discussion

We're currently not considering area or population for ordering, so anything is an improvement here. It's hard to say if area,population or population,area is better without trying one first.
  • Loading branch information
pnorman committed May 16, 2014
1 parent 1b788bf commit 98e712a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion project.mml
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@
],
"Datasource": {
"type": "postgis",
"table": "(SELECT way,place,capital,name\n FROM planet_osm_point\n WHERE place IN ('city','town')) AS placenames_medium",
"table": "(SELECT way,place,name,capital,population\n FROM\n (SELECT \n way,place,name,NULL AS capital,way_area,\n CASE WHEN population~E'^\\d{1,9}$' THEN population::integer ELSE NULL END AS population\n FROM planet_osm_polygon\n UNION ALL\n SELECT \n way,place,name,capital,NULL AS way_area,\n CASE WHEN population~E'^\\d{1,9}$' AND length(population)<10 THEN population::integer ELSE NULL END AS population \n FROM planet_osm_point) AS p\n WHERE place IN ('city','town')\n ORDER BY \n CASE place WHEN 'city' THEN 0 ELSE 10 END,\n CASE capital WHEN 'yes' THEN 0 ELSE 10 END, \n way_area DESC NULLS LAST, \n population DESC NULLS LAST\n) AS placenames_medium",
"extent": "-20037508,-19929239,20037508,19929239",
"key_field": "",
"geometry_field": "way",
Expand Down

0 comments on commit 98e712a

Please sign in to comment.