Skip to content

Instantly share code, notes, and snippets.

@gregmacfarlane
Created May 6, 2022 15:15
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 gregmacfarlane/9d8808a2518841449a72ab9e8069676b to your computer and use it in GitHub Desktop.
Save gregmacfarlane/9d8808a2518841449a72ab9e8069676b to your computer and use it in GitHub Desktop.
Generate an OSM PBF from link and node csv tables
package beam.utils;
import com.conveyal.osmlib.Node;
import com.conveyal.osmlib.OSM;
import com.conveyal.osmlib.Way;
import com.univocity.parsers.common.record.Record;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import org.matsim.core.utils.misc.Counter; // This creates a geometric counter that helped me keep track of things, but isn't necessary
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
public class LinkTablesToOSMConverter {
private static final Logger logger = LoggerFactory.getLogger(LinkTablesToOSMConverter.class);
HashMap<Integer, Node> nodeHashMap = new HashMap<>();
HashMap<String, Way> wayHashMap = new HashMap<>();
Counter nodesCounter = new Counter("Read node " );
Counter linksCounter = new Counter("Read link ");
OSM osm = null;
public LinkTablesToOSMConverter(File nodesTable, File linksTable, File outFile) throws IOException {
this.osm = new OSM(null);
readNodes(nodesTable);
readLinks(linksTable);
osm.writeToFile(outFile.getPath());
}
private void readNodes(File nodesTable) {
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
parser.beginParsing(nodesTable);
Record record;
parser.getRecordMetadata();
while ((record = parser.parseNextRecord()) != null) {
long id = record.getInt("id");
Double x = record.getDouble("x");
Double y = record.getDouble("y");
osm.nodes.put(id, new Node(x, y));
nodesCounter.incCounter();
}
logger.info("Read nodes: " + nodesCounter.getCounter());
}
private void readLinks(File linksFile) {
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
parser.beginParsing(linksFile);
Record record;
parser.getRecordMetadata();
while ((record = parser.parseNextRecord()) != null) {
Way way = new Way();
String id = record.getString("link_id");
long start = record.getLong("a");
long end = record.getLong("b");
way.addTag("length", record.getString("length"));
way.addTag("speed", record.getString("speed"));
way.addTag("type", record.getString("type"));
way.addTag("lanes", record.getString("lanes"));
way.addTag("capacity", record.getString("capacity"));
long[] nodes = new long[2];
nodes[0] = start;
nodes[1] = end;
way.nodes = nodes;
linksCounter.incCounter();
osm.ways.put(linksCounter.getCounter(), way);
}
logger.info("Read links: " + linksCounter.getCounter());
}
public static void main(String[] args) throws Exception {
File linksTable = new File(args[0]);
File nodesTable = new File(args[1]);
File outFile = new File(args[2]);
logger.info("==== Link tables to OSM converter ====");
logger.info("Links file: " + linksTable.toString());
logger.info("Nodes file: " + nodesTable.toString());
LinkTablesToOSMConverter converter = new LinkTablesToOSMConverter(
nodesTable, linksTable, outFile);
}
}
@gregmacfarlane
Copy link
Author

The environment for this is within BEAM, but the only dependencies are the univocity csv parser and the conveyal osm libraries.

Called with three program arguments:

links.csv nodes.csv output.osm.pbf

The files I'm using are below:

links file
nodes file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment