Saturday 4 May 2019

carto - Merge polygons in CartoDB?


I created this map in cartodb: http://cdb.io/1xBj4Xo


Now I have to merge some of the districts to a bigger district. Is it possible to do this in Cartodb? Using a SQL query for instance?



I can also provide a .csv-file: http://tradukt.ch/owncloud/public.php?service=files&t=2fe413428dd38103705fa4191236b8fc


Screenshot of the table:


enter image description here



Answer



Based on your CSV,


Tablename: Wahlkreise
Column containing value to union on: wahlkreis
Other Columns use an aggregate function to grab one of the values (all of them are the same for each district / wahlkreis


In the SQL window in CartoDB, type:


   SELECT   


wahlkreis
, cartodb_id
, max(strong_party) as strong_party
, max(einwohner) as einwohner
, max(mandate) as mandate
, max(kandidaten) as kandidaten
, max(frauen) as frauen
, max(maenner) as maenner
, max(fdp) as fdp

, max(svp) as svp
, max(sp) as sp
, max(glp) as glp
, max(gruene) as gruene
, max(evp) as evp
, max(cvp) as cvp
, max(bdp) as bdp

, st_union(the_geom_webmercator) as the_geom_webmercator



FROM wahlkreise_bl_1

group by wahlkreis, wahlkreise_bl_1.cartodb_id

At this point, map interaction must be enabled by adding the CartoDB_id to the SQL and GROUP BY clause, but there seems to be a bug that removes the st_union function from working properly (I will keep digging in).


The workflow instead could be to simply use the 'create table from query' option to write your query to a new file, and you can take advantage of the full interactivity based on your SQL processing.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...