{"id":26190888,"url":"https://github.com/codingabi/population-density-mapnik","last_synced_at":"2026-01-31T12:34:12.339Z","repository":{"id":166939825,"uuid":"186615253","full_name":"codingABI/population-density-mapnik","owner":"codingABI","description":"An population per square kilormetre map based on OpenStreetMap data","archived":false,"fork":false,"pushed_at":"2020-08-07T18:55:47.000Z","size":8112,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-17T19:04:35.588Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codingABI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-05-14T12:15:02.000Z","updated_at":"2020-08-07T18:55:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"820fb559-1db6-436d-917c-34adf700fb83","html_url":"https://github.com/codingABI/population-density-mapnik","commit_stats":null,"previous_names":["codingabi/population-density-mapnik"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codingABI/population-density-mapnik","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingABI%2Fpopulation-density-mapnik","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingABI%2Fpopulation-density-mapnik/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingABI%2Fpopulation-density-mapnik/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingABI%2Fpopulation-density-mapnik/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codingABI","download_url":"https://codeload.github.com/codingABI/population-density-mapnik/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingABI%2Fpopulation-density-mapnik/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28943204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T12:10:04.904Z","status":"ssl_error","status_checked_at":"2026-01-31T12:09:58.894Z","response_time":128,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-12T00:58:31.193Z","updated_at":"2026-01-31T12:34:12.333Z","avatar_url":"https://github.com/codingABI.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# population-density-mapnik\nA map style for displaying population density with mapnik based on OpenStreetMap data.\n\nThis map shows the population density in the OpenStreetMap germany extract (https://download.geofabrik.de/europe/germany.html) and the named areas with the highest and lowest density:\n![alt text](https://github.com/codingABI/population-density-mapnik/blob/master/population-density.png) \n\n## Workflow to determine population data for boundaries:\n- Search for boundary-areas with admin_level 4, 6 or 8\n- If the boundary-area has a population-tag the needed data is found\n- When the boundary-area has no population-tag and the boundary has no multiple outers, search for a place-node from type \"municipality\",\"borough\",\"suburb\",\"city\",\"town\" or \"village\" within the area which has a population-tag and the same name as the area. If such a place-node is found the population of the node is used for the boundary-area and the needed data is found too.\n- When the boundary-area has no population-tag and the boundary has multiple outers, search for unique place-node from type \"municipality\",\"borough\",\"suburb\",\"city\",\"town\" or \"village\" within all outers which has a population-tag and the same name as the area. If such a place-node is found the population of the node is used for the boundary-area and the needed data is found too.\n- With the found population data and the size for the boundary-area the population density is calculated\n- Use orange for boundary-areas with admin_level 6 or 8 and existing name and where no population was found\n- Use different shades for green for the density value\n\n## Detailed data processing: \nImport osm data with osm2pgsql to postgis database ([osm2pgsql-Style](population-density.style)) and open psql to start SQL statements. The import is time consuming and my notebook needs ~24h.\n```\nosm2pgsql -d population -r pbf --create --cache 1024 -S population-density.style -s --number-processes 1 germany-latest.osm.pbf\npsql population\n```\nSQL statements to create the final table **tPopulationDensity**:\n```\n--Helper table 1: boundaries with multiple outers and population on boundary\nDROP Table tMultiOuterPopulationOnPoly;\nCREATE Temp TABLE tMultiOuterPopulationOnPoly as (SELECT area.way, area.way_area, area.osm_id, area.name FROM planet_osm_polygon as area WHERE area.boundary='administrative' and area.admin_level IN ('4','6','8') and area.population similar to '[0-9]+' and exists (select multi_outer_check.osm_id from planet_osm_polygon as multi_outer_check where multi_outer_check.osm_id = area.osm_id group by multi_outer_check.osm_id having count(*) \u003e 1));\n\n--Helper table 2: sum all areas for boundaries with one or more outers\nDROP Table tAreaSumKm2;\nCREATE Temp TABLE tAreaSumKm2 as (SELECT area.osm_id, area.name, Sum(ST_Area(ST_Transform(area.way,3035))/1000000) as area_km2 FROM planet_osm_polygon as area WHERE area.boundary='administrative' and area.admin_level IN ('4','6','8') group by area.osm_id, area.name);\n\n--Helper table 3: boundaries with multiple outers and no poplation on boundary, but population on node\nDrop Table tMultiOuterPopulationOnNode;\nCREATE Temp TABLE tMultiOuterPopulationOnNode as (SELECT area.way, area.way_area, area.name, area.osm_id, point.population FROM planet_osm_polygon as area FULL OUTER JOIN planet_osm_point as point ON st_contains(area.way,point.way) and point.place IN ('municipality','borough','suburb','city','town','village') and area.name=point.name and point.population similar to '[0-9]+' WHERE area.boundary='administrative' and area.admin_level IN ('4','6','8') and (area.population is null or  area.population not similar to '[0-9]+') and exists (select multi_outer_check.osm_id from planet_osm_polygon as multi_outer_check where multi_outer_check.osm_id = area.osm_id group by multi_outer_check.osm_id having count(*) \u003e 1));\n\n--Helper table 4: list of unique population of boundaries with multiple outers and no poplation on boundary, but population on node\nDrop Table tMultiOuterPopulationOnUniqueNode;\nCREATE Temp TABLE tMultiOuterPopulationOnUniqueNode as (select osm_id, name, population, count(population) from tMultiOuterPopulationOnNode where population similar to '[0-9]+' group by osm_id, name, population having count(*) = 1);\n\n--Create main table and insert helper tables\nDROP TABLE tPopulationDensity;\nCREATE TABLE tPopulationDensity as (select tMultiOuterPopulationOnPoly.way, tMultiOuterPopulationOnPoly.way_area, tMultiOuterPopulationOnPoly.name, tMultiOuterPopulationOnPoly.osm_id, Round(area.population::Integer/tAreaSumKm2.area_km2) as population_per_km2, area.population::Integer as population, tAreaSumKm2.area_km2 from tMultiOuterPopulationOnPoly, tAreaSumKm2,planet_osm_polygon as area where tMultiOuterPopulationOnPoly.osm_id = tAreaSumKm2.osm_id and tMultiOuterPopulationOnPoly.osm_id = area.osm_id);\n\nINSERT INTO tPopulationDensity SELECT tMultiOuterPopulationOnNode.way, tMultiOuterPopulationOnNode.way_area, tMultiOuterPopulationOnNode.name, tMultiOuterPopulationOnNode.osm_id, Round(tMultiOuterPopulationOnUniqueNode.population::Integer / tAreaSumKm2.area_km2), tMultiOuterPopulationOnUniqueNode.population::Integer as population, tAreaSumKm2.area_km2  FROM tMultiOuterPopulationOnNode,tMultiOuterPopulationOnUniqueNode,tAreaSumKm2 where tMultiOuterPopulationOnNode.osm_id = tMultiOuterPopulationOnUniqueNode.osm_id and tMultiOuterPopulationOnNode.osm_id = tAreaSumKm2.osm_id;\n\n--Insert all boundaries with one outer and population on boundary or node\nINSERT INTO tPopulationDensity SELECT area.way, area.way_area, area.name, area.osm_id, Round(COALESCE(area.population,point.population)::Integer/(ST_Area(ST_Transform(area.way,3035))/1000000)) as population_per_km2, COALESCE(area.population,point.population)::Integer as population, ST_Area(ST_Transform(area.way,3035))/1000000 as area_km2 FROM planet_osm_polygon as area FULL OUTER JOIN planet_osm_point as point ON st_contains(area.way,point.way) and point.place IN ('municipality','borough','suburb','city','town','village') and area.name=point.name and point.population similar to '[0-9]+' WHERE area.boundary='administrative' and area.admin_level IN ('4','6','8') and (area.population similar to '[0-9]+' or point.population similar to '[0-9]+') and exists (select multi_outer_check.osm_id from planet_osm_polygon as multi_outer_check where multi_outer_check.osm_id = area.osm_id group by multi_outer_check.osm_id having count(*) = 1);\n\n--Insert boundaries with names and admin_level 6 or 8 and without population (set to null)\nINSERT INTO tPopulationDensity SELECT area.way, area.way_area, area.name, area.osm_id, null as population_per_km2, null as population, tAreaSumKm2.area_km2 FROM planet_osm_polygon as area, tAreaSumKm2 where area.boundary='administrative' and area.admin_level IN ('6','8') and area.name is not null and area.osm_id = tAreaSumKm2.osm_id and not exists (select tPopulationDensity.osm_id from tPopulationDensity Where tPopulationDensity.osm_id = area.osm_id); \n\n--When creating a map for a specific country, it may be a good idea to filter only areas for the country (uncomment following 4 lines)\n--DROP TABLE tPopulationDensityDE;\n--CREATE Temp TABLE tPopulationDensityDE as (select tPopulationDensity.* from tPopulationDensity, planet_osm_polygon as border where ST_Contains(border.way,tPopulationDensity.way) and border.name = 'Deutschland' and border.admin_level = '2' and border.boundary='administrative');\n--DROP TABLE tPopulationDensity;\n--CREATE TABLE tPopulationDensity as (select * from tPopulationDensityDE);\n\n--Add column for biggest and smallest population density\nalter table tPopulationDensity add column flags varchar;\nupdate tPopulationDensity SET flags = 'max' where population_per_km2 = (select max(population_per_km2) from tPopulationDensity);\nupdate tPopulationDensity SET flags = 'min' where population_per_km2 = (select min(population_per_km2) from tPopulationDensity);\n```\nNow you can use renderd or an [Python-Script](population-density.py) to create a map ([mapnik-XML](population-density.xml)).\n\nThis should only be a demonstration how to process such OpenStreetMap data. If you really want exact and current population data, you should ask your goverment for official data.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodingabi%2Fpopulation-density-mapnik","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodingabi%2Fpopulation-density-mapnik","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodingabi%2Fpopulation-density-mapnik/lists"}