This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

Overpass: Optimization of a Colored Postal Code Query

0

Hello,

I hope you're all doing well!

First of all, unfortunately, my knowledge regarding javascript query building in Overpass is relatively limited. I would like to create a map of German postalcode areas that are colored differently. It can happen that several postal code areas should have the same color. The following code is already functioning:

[out:json][timeout:25];

{{geocodeArea:Germany}}->.searchArea;

// gather results
(      
  relation["postal_code"="45529"](area.searchArea);
  relation["postal_code"="45527"](area.searchArea);  
  relation["postal_code"="45525"](area.searchArea);
);


{{style:
  relation[postal_code=45529]
    { color:red; fill-color:red; text: note;}
  relation[postal_code=45527]
    { color:red; fill-color:red; text: note;}
  relation[postal_code=45525]
    { color:yellow; fill-color:yellow; text: note;}
}}

// print results
out body;
>;
out skel qt;

However, I'm pretty sure that there is room for improvement for shortening the code and, therefore, the URL link created from this query. Maybe it is possible to place several postal codes in one relation? Maybe the style code can be improved to merge the postal codes with the same color?

Thank you very much in advance!

All the best Josh

asked 31 Oct '22, 09:39

Joshua1511's gravatar image

Joshua1511
0112
accept rate: 0%


One Answer:

1
{{geocodeArea:Germany}};rel[postal_code~"45529|45527|45525"](area);{{style:relation{fill-color:red;text:note}relation[postal_code=45525]{fill-color:yellow} }}out geom;
  • Combine search for similar entities into one command.
  • Use 'rel' instead of relation
  • Remove unnecessary parenthesis
  • Remove timeout
  • Remove out:json if you're not interested in data format
  • use out geom; if you're not interested in data format
  • Remove whitespace
  • Remove all comments
  • Remove carriage returns so it's all on one line (note space is required between the style closing curly brackets)
  • Remove comma after 'note' & 'yellow'
  • Is color: required?

MapCss works on a default system so you can specify a colour for the relations you want red & specify postal_codes for those with a different colour. If you want labels to display in all areas it only needs to be declared once.

In your specific example there's no requirement to search using the area as the postal_codes are unique, but that might no be true in every case.

In your specific example you could also reduce the search to postal_code~"4552."

answered 31 Oct '22, 11:02

DaveF's gravatar image

DaveF
3.3k8498133
accept rate: 16%

edited 31 Oct '22, 11:03

This is awesome! Thank you very much! I'll play a bit around with your code.

(31 Oct '22, 11:09) Joshua1511

Source code available on GitHub .