Parse an OpenStreetMap osm file to produce a csv of nodes and lat/long

Post your nifty GP projects here!

Moderator: MSandro

Post Reply
SimpleSi
Posts: 327
Joined: Jul 2nd, '17, 13:47

Parse an OpenStreetMap osm file to produce a csv of nodes and lat/long

Post by SimpleSi » Aug 19th, '17, 16:39

This is my first "real" (as in I needed to code it and chose to do it in GP instead of Python) GP program

I had a csv of the outline of main British coastline (it was made about 35 years ago by actually entering in UK national grid references into a spreadsheet to run on a Unisys BTOS workstation to drive an HP plotter to draw the coast outline and then plot other objects as well)

But I wanted to add in Northern Ireland and NI uses Eire grid ref system that is not compatible with British one so I decided to extract make a low resoultion osm layer of the NI coast

The format of a simple osm xml file is a list of nodes with lat/long info and ways (roads/coastlines etc) that are a list of node ids)

Code: Select all

<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="osmfilter 1.4.2">
	<node id="28537534" lat="54.09694" lon="-6.1977291" version="2" timestamp="2010-05-18T01:05:06Z" changeset="4734869" uid="266276" user="Steve_NI">
		<tag k="source" v="PGS"/>
	</node>
	<node id="28537622" lat="54.1007576" lon="-6.2388156" version="2" timestamp="2010-05-02T00:02:13Z" changeset="4580557" uid="266276" user="Steve_NI">
		<tag k="source" v="PGS"/>
	</node>
	.....
	
	<way id="-90080">
		<nd ref="28589200"/>
		<nd ref="28589495"/>
		<nd ref="28589668"/>
		<nd ref="28615055"/>
		<nd ref="1286129971"/>
		<nd ref="3509181080"/>
		
		....	
	
But I needed a flat csv file consiting of following format
Penup/down,anything,anything,anything,long,lat

to be compatible with my old csv data for mainland Britain

This is my final working code
It basically parses each line extracting anything inside "" and outputs them to a fixed field sized string and then allocates the fields to different lists depending on whether a node id or a way id

Output class runs thru the lists producing the final .csv file

Edit
Discovered the wonders of using lines to make parsing a text file much easier so this is an updated version
osmread4.gpp
(52.69 KiB) Downloaded 49 times

Post Reply