Created
April 27, 2016 06:13
-
-
Save acmi/2f822641c67bf365e22c1a83c40737bc to your computer and use it in GitHub Desktop.
roads sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import engine.osm.xml.Node; | |
import engine.osm.xml.Way; | |
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBElement; | |
import javax.xml.bind.Unmarshaller; | |
import javax.xml.stream.XMLInputFactory; | |
import javax.xml.stream.XMLStreamReader; | |
import javax.xml.stream.events.XMLEvent; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
public class T1 { | |
public static void main(String[] args) throws Exception { | |
XMLInputFactory xif = XMLInputFactory.newFactory(); | |
JAXBContext jc = JAXBContext.newInstance(Way.class); | |
Unmarshaller unmarshaller = jc.createUnmarshaller(); | |
try (InputStream is = new BZip2CompressorInputStream(T1.class.getResourceAsStream("/RU-UD.osm.bz2"))) { | |
int fi = 0; | |
int ni = 0; | |
PrintWriter writer = new PrintWriter("C:\\sql\\way_nodes_" + fi + ".sql"); | |
writer.println("INSERT INTO `way_nodes` (`way_id`,`node_id`,`node_index`) VALUES"); | |
XMLStreamReader xsr = xif.createXMLStreamReader(is); | |
while (xsr.hasNext()) { | |
int event = xsr.next(); | |
if (event == XMLEvent.END_DOCUMENT) | |
break; | |
else if (event == XMLEvent.START_ELEMENT) | |
if (xsr.getLocalName().equals("way")) { | |
JAXBElement<Way> jb = unmarshaller.unmarshal(xsr, Way.class); | |
Way way = jb.getValue(); | |
for (int i=0; i<way.nd.size()-1; i++) | |
writer.println("(" + way.id + "," + way.nd.get(i).ref + "," + i + "),"); | |
writer.print("(" + way.id + "," + way.nd.get(way.nd.size()-1).ref + "," + (way.nd.size()-1) + ")"); | |
if (ni++ < 20_000) | |
writer.println(","); | |
else { | |
writer.println(";"); | |
writer.close(); | |
writer = new PrintWriter("C:\\sql\\way_nodes_" + (++fi) + ".sql"); | |
writer.println("INSERT INTO `way_nodes` (`way_id`,`node_id`,`node_index`) VALUES"); | |
ni = 0; | |
} | |
} | |
} | |
writer.close(); | |
xsr.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment