Skip to content

Instantly share code, notes, and snippets.

@karlwettin
Created March 20, 2020 18:19
Show Gist options
  • Save karlwettin/fe1e60653b7f71423e64a48dcef29d6b to your computer and use it in GitHub Desktop.
Save karlwettin/fe1e60653b7f71423e64a48dcef29d6b to your computer and use it in GitHub Desktop.
package se.kodapan.osm.prerender;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import se.kodapan.osm.domain.Node;
import se.kodapan.osm.domain.Relation;
import se.kodapan.osm.domain.Way;
import se.kodapan.osm.parser.xml.streaming.StreamingOsmXmlParser;
import se.kodapan.osm.parser.xml.streaming.StreamingOsmXmlParserListener;
import se.kodapan.osm.util.slippymap.Tile;
import se.kodapan.osm.util.slippymap.TileVisitor;
import se.kodapan.osm.util.slippymap.XYZ;
import java.io.*;
import java.util.*;
public class PreRenderTilesGatherer {
public static void main(String[] args) throws Exception {
extractPlaces(new File("data/extracted-places.osm.xml"));
}
public static void extractPlaces(File osmFile) throws Exception {
File outputFile = new File(osmFile.getParent(), osmFile.getName() + ".places-tiles.txt");
XYZ slippymap = new XYZ();
Set<Tile> tiles = new LinkedHashSet<>(500000);
StreamingOsmXmlParser parser = StreamingOsmXmlParser.newInstance();
try {
parser.read(new InputStreamReader(new FileInputStream(osmFile), "UTF8"), new StreamingOsmXmlParserListener() {
@Override
public void processParsedNode(Node node) {
if (node.getTag("place") != null) {
for (int z = 1; z < 18; z++) {
Tile tile = slippymap.tileFactory(node.getLongitude(), node.getLatitude(), z);
tiles.add(tile);
}
}
}
@Override
public void processParsedWay(Way way) {
throw new RuntimeException("Breaking out"); // todo bbox, but the nodes here are only refs
}
@Override
public void processParsedRelation(Relation relation) {
throw new RuntimeException("Breaking out"); // todo bbox, but the items here are only refs
}
});
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Writing");
Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF8");
for(Tile tile : tiles) {
out.write(String.valueOf(tile.getX()));
out.write("\t");
out.write(String.valueOf(tile.getY()));
out.write("\t");
out.write(String.valueOf(tile.getZ()));
out.write("\n");
}
out.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment