Skip to content

Commit

Permalink
Merge pull request #69 from pelias/optional_way_nodes
Browse files Browse the repository at this point in the history
put output of way nodes behind cli flag, disable by default
  • Loading branch information
missinglink authored Nov 1, 2018
2 parents 0788d03 + db6cf60 commit fa9e5c7
Show file tree
Hide file tree
Showing 13 changed files with 398,999 additions and 2,221,524 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ If you need to target only specific values for a tag you can specify exactly whi

### Denormalization

When processing the ways, the node refs are looked up for you and the lat/lon values are added to each way:
When processing the ways, the node refs are looked up for you and the lat/lon values are added to each way.

Since version `3.0` centroids are also computed for each way.
Since version `3.0` centroids are also computed for each way, since version `5.0` bounds are now also computed.

Output of the `nodes` array (as seen below) is optional, this was disabled by default in version `5.0` but can be enabled with the flag `--waynodes=true`.

```bash
{
Expand All @@ -88,6 +90,12 @@ Since version `3.0` centroids are also computed for each way.
"lat": "51.554679",
"lon": "-0.098485"
},
"bounds": {
"e": "-0.0983673",
"n": "51.5547179",
"s": "51.5546574",
"w": "-0.0985915"
},
"nodes": [
{
"lat": "51.554663",
Expand Down
Binary file modified build/pbf2json.darwin-x64
Binary file not shown.
Binary file modified build/pbf2json.linux-arm
Binary file not shown.
Binary file modified build/pbf2json.linux-x64
Binary file not shown.
Binary file modified build/pbf2json.win32-x64
Binary file not shown.
45 changes: 31 additions & 14 deletions pbf2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ type settings struct {
LevedbPath string
Tags map[string][]string
BatchSize int
WayNodes bool
}

var emptyLatLons = make([]map[string]string, 0)

func getSettings() settings {

// command line flags
leveldbPath := flag.String("leveldb", "/tmp", "path to leveldb directory")
tagList := flag.String("tags", "", "comma-separated list of valid tags, group AND conditions with a +")
batchSize := flag.Int("batch", 50000, "batch leveldb writes in batches of this size")
wayNodes := flag.Bool("waynodes", false, "should the lat/lons of nodes belonging to ways be printed")

flag.Parse()
args := flag.Args()
Expand All @@ -54,7 +58,7 @@ func getSettings() settings {
// fmt.Print(conditions, len(conditions))
// os.Exit(1)

return settings{args[0], *leveldbPath, conditions, *batchSize}
return settings{args[0], *leveldbPath, conditions, *batchSize, *wayNodes}
}

func main() {
Expand Down Expand Up @@ -152,9 +156,13 @@ func run(d *osmpbf.Decoder, db *leveldb.DB, config settings) {
}

// compute centroid
var centroid = computeCentroid(latlons)
centroid, bounds := computeCentroidAndBounds(latlons)

onWay(v, latlons, centroid)
if config.WayNodes {
onWay(v, latlons, centroid, bounds)
} else {
onWay(v, emptyLatLons, centroid, bounds)
}
}

case *osmpbf.Relation:
Expand Down Expand Up @@ -195,11 +203,20 @@ type jsonWay struct {
Tags map[string]string `json:"tags"`
// NodeIDs []int64 `json:"refs"`
Centroid map[string]string `json:"centroid"`
Nodes []map[string]string `json:"nodes"`
Bounds map[string]string `json:"bounds"`
Nodes []map[string]string `json:"nodes,omitempty"`
}

func onWay(way *osmpbf.Way, latlons []map[string]string, centroid map[string]string) {
marshall := jsonWay{way.ID, "way", way.Tags /*, way.NodeIDs*/, centroid, latlons}
func onWay(way *osmpbf.Way, latlons []map[string]string, centroid map[string]string, bounds *geo.Bound) {

// render a North-South-East-West bounding box
var bbox = make(map[string]string)
bbox["n"] = strconv.FormatFloat(bounds.North(), 'f', 7, 64)
bbox["s"] = strconv.FormatFloat(bounds.South(), 'f', 7, 64)
bbox["e"] = strconv.FormatFloat(bounds.East(), 'f', 7, 64)
bbox["w"] = strconv.FormatFloat(bounds.West(), 'f', 7, 64)

marshall := jsonWay{way.ID, "way", way.Tags /*, way.NodeIDs*/, centroid, bbox, latlons}
json, _ := json.Marshal(marshall)
fmt.Println(string(json))
}
Expand Down Expand Up @@ -454,8 +471,8 @@ func selectEntrance(entrances []map[string]string) map[string]string {
return centroid
}

// compute the centroid of a way
func computeCentroid(latlons []map[string]string) map[string]string {
// compute the centroid of a way and its bbox
func computeCentroidAndBounds(latlons []map[string]string) (map[string]string, *geo.Bound) {

// check to see if there is a tagged entrance we can use.
var entrances []map[string]string
Expand All @@ -465,11 +482,6 @@ func computeCentroid(latlons []map[string]string) map[string]string {
}
}

// use the mapped entrance location where available
if len(entrances) > 0 {
return selectEntrance(entrances)
}

// convert lat/lon map to geo.PointSet
points := geo.NewPointSet()
for _, each := range latlons {
Expand All @@ -478,6 +490,11 @@ func computeCentroid(latlons []map[string]string) map[string]string {
points.Push(geo.NewPoint(lon, lat))
}

// use the mapped entrance location where available
if len(entrances) > 0 {
return selectEntrance(entrances), points.Bound()
}

// determine if the way is a closed centroid or a linestring
// by comparing first and last coordinates.
isClosed := false
Expand All @@ -498,5 +515,5 @@ func computeCentroid(latlons []map[string]string) map[string]string {
centroid["lat"] = strconv.FormatFloat(compute.Lat(), 'f', 7, 64)
centroid["lon"] = strconv.FormatFloat(compute.Lng(), 'f', 7, 64)

return centroid
return centroid, points.Bound()
}
Loading

0 comments on commit fa9e5c7

Please sign in to comment.