Skip to content

Instantly share code, notes, and snippets.

@seeebiii
Last active February 25, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seeebiii/d929a2ee2601791554d315f212164ed6 to your computer and use it in GitHub Desktop.
Save seeebiii/d929a2ee2601791554d315f212164ed6 to your computer and use it in GitHub Desktop.
A simple Java class to convert Nominatim special phrases. Outputs data as a key with list of values. Output is stored in JSON file.
package de.sebastianhesse.pbf.reader;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Converts a file containing Nominatim special phrases.
* Result is stored as JSON, so a frontend script can easily read the values.
* Data extracted from http://wiki.openstreetmap.org/w/index.php?title=Nominatim/Special_Phrases/DE
*/
public class NominatimSpecialPhrasesConverter {
public static void main(String[] args) {
// prepare input and output file
String filepath = args[0];
if (StringUtils.isBlank(filepath)) {
System.out.println("You must provide a filename.");
System.exit(1);
}
String outputFilepath = StringUtils.removeEndIgnoreCase(filepath, ".txt") + ".json";
File originalFile = new File(filepath);
File outputFile = new File(outputFilepath);
resetFileIfExists(outputFile);
// read each line and put key value pairs into a map. each key can have multiple values, thus Set<String>
LineIterator lineIterator = null;
Map<String, Set<String>> phrases = new HashMap<>();
try (FileReader reader = new FileReader(originalFile); FileWriter writer = new FileWriter(outputFile)){
lineIterator = IOUtils.lineIterator(reader);
while (lineIterator.hasNext()) {
String line = lineIterator.nextLine();
// ignore comments and other lines, just parse lines with phrases
if (StringUtils.startsWith(line, "| ")) {
handleKeyValuePair(phrases, line);
}
}
// write phrases to output file as json
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, phrases);
} catch (IOException e) {
System.out.println("Something went wrong when reading/writing the input/output file.");
e.printStackTrace();
}
}
private static void resetFileIfExists(File outputFile) {
if (outputFile.exists()) {
outputFile.delete();
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
private static void handleKeyValuePair(Map<String, Set<String>> phrases, String line) {
// extract key and value pair
String[] parts = StringUtils.split(line, "||");
String key = parts[1].trim();
String value = parts[2].trim();
// add value to list of values for key
Set<String> values = null;
if (phrases.containsKey(key)) {
values = phrases.get(key);
} else {
// first value to add, thus creating a new Set
values = new HashSet<>();
phrases.put(key, values);
}
values.add(value);
}
}
{
"boundary": [
"administrative"
],
"historic": [
"archaeological_site",
"mine",
"wayside_shrine",
"castle",
"wayside_cross",
"monument",
"building",
"manor",
"wreck",
"battlefield",
"ruins",
"boundary_stone",
"memorial"
],
"shop": [
"gift",
"insurance",
"electronics",
"beauty",
"bicycle",
"travel_agency",
"butcher",
"convenience",
"car_repair",
"copyshop",
"deli",
"motorcycle",
"shopping_centre",
"music",
"charity",
"furniture",
"greengrocer",
"farm",
"florist",
"funeral_directors",
"stationery",
"newsagent",
"tobacco",
"massage",
"pet",
"hardware",
"alcohol",
"jewelry",
"beverages",
"bakery",
"mall",
"tattoo",
"books",
"hifi",
"carpet",
"erotic",
"wine",
"salon",
"department_store",
"confectionery",
"video",
"supermarket",
"general",
"computer",
"hairdresser",
"car",
"laundry",
"mobile_phone",
"garden_centre",
"estate_agent",
"chemist",
"organic",
"seafood",
"doityourself",
"art",
"toys",
"sports",
"photo",
"optician",
"shoes",
"clothes",
"food",
"outdoor",
"car_parts",
"fish",
"dry_cleaning",
"kiosk",
"fashion"
],
"natural": [
"scree",
"cliff",
"cave_entrance",
"fell",
"heath",
"rock",
"spring",
"land",
"wood",
"bay",
"volcano",
"ridge",
"glacier",
"marsh",
"shoal",
"reef",
"coastline",
"wetland",
"tree",
"peak",
"scrub",
"water",
"moor",
"beach",
"valley",
"cape",
"mud"
],
"man_made": [
"windmill",
"water_well"
],
"amenity": [
"college",
"studio",
"fountain",
"public_building",
"kneipp_water_cure",
"place_of_worship",
"fuel",
"casino",
"ice_cream",
"post_box",
"theatre",
"motorcycle_parking",
"bank",
"bar",
"hunting_stand",
"school",
"courthouse",
"atm",
"cinema",
"community_centre",
"prison",
"nightclub",
"nursing_home",
"arts_centre",
"biergarten",
"bus_station",
"bench",
"taxi",
"bureau_de_change",
"telephone",
"bicycle_rental",
"dentist",
"charging_station",
"toilets",
"cafe",
"bicycle_parking",
"doctors",
"ferry_terminal",
"pharmacy",
"car_rental",
"embassy",
"gym",
"fast_food",
"dojo",
"waste_basket",
"kindergarten",
"swingerclub",
"animal_boarding",
"shelter",
"grit_bin",
"university",
"brothel",
"car_sharing",
"emergency_phone",
"library",
"driving_school",
"sauna",
"hospital",
"vending_machine",
"townhall",
"car_wash",
"register_office",
"restaurant",
"post_office",
"recycling",
"veterinary",
"retirement_home",
"fire_station",
"crematorium",
"police",
"grave_yard",
"drinking_water",
"clinic",
"pub"
],
"waterway": [
"weir",
"derelict_canal",
"rapids",
"ditch",
"riverbank",
"drain",
"dam",
"boatyard",
"stream",
"water_point",
"waterfall",
"wadi",
"canal",
"river",
"dock"
],
"mountain_pass": [
"yes"
],
"emergency": [
"fire_hydrant"
],
"tourism": [
"guest_house",
"viewpoint",
"hostel",
"chalet",
"artwork",
"motel",
"attraction",
"museum",
"alpine_hut",
"caravan_site",
"hotel",
"camp_site",
"information",
"theme_park",
"zoo",
"picnic_site"
],
"building": [
"commercial",
"shop",
"city_hall",
"university",
"church",
"dormitory",
"hall",
"office",
"house",
"retail",
"faculty",
"public",
"flats",
"school",
"train_station",
"farm",
"hotel",
"stadium",
"block",
"terrace",
"entrance",
"hospital",
"tower",
"bunker",
"yes",
"garage",
"store",
"chapel",
"industrial",
"residential",
"apartments"
],
"landuse": [
"commercial",
"forest",
"farmyard",
"brownfield",
"farmland",
"retail",
"landfill",
"piste",
"vineyard",
"grass",
"construction",
"farm",
"military",
"wood",
"railway",
"cemetery",
"meadow",
"greenfield",
"village_green",
"allotments",
"basin",
"quarry",
"conservation",
"industrial",
"residential",
"recreation_ground",
"reservoir"
],
"place": [
"country",
"town",
"city",
"hamlet",
"island",
"county",
"locality",
"municipality",
"islet",
"house",
"sea",
"houses",
"farm",
"suburb",
"state",
"region",
"village"
],
"railway": [
"tram_stop",
"light_rail",
"disused",
"platform",
"monorail",
"switch",
"funicular",
"halt",
"level_crossing",
"subway",
"preserved",
"station",
"construction",
"abandoned",
"tram",
"narrow_gauge",
"subway_entrance"
],
"highway": [
"unclassified",
"minor",
"primary_link",
"bus_guideway",
"ford",
"distance_marker",
"platform",
"rest_area",
"path",
"raceway",
"trail",
"road",
"construction",
"secondary_link",
"track",
"unsurfaced",
"emergency_access_point",
"byway",
"bridleway",
"tertiary",
"living_street",
"services",
"stile",
"motorway_link",
"trunk",
"motorway",
"steps",
"secondary",
"residential",
"motorway_junction",
"service",
"footway",
"pedestrian",
"bus_stop",
"gate",
"cycleway",
"trunk_link",
"primary"
],
"leisure": [
"ice_rink",
"sports_centre",
"fishing",
"marina",
"beach_resort",
"common",
"water_park",
"recreation_ground",
"playground",
"stadium",
"garden",
"miniature_golf",
"nature_reserve",
"pitch",
"slipway",
"track",
"golf_course",
"park",
"swimming_pool"
],
"aeroway": [
"aerodrome"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment