Skip to content

Commit

Permalink
feat(relations): fix bug where rel->way->node nodes were not being in…
Browse files Browse the repository at this point in the history
…dexed
  • Loading branch information
missinglink committed Apr 25, 2019
1 parent 6a168ef commit 4445719
Show file tree
Hide file tree
Showing 11 changed files with 2,598 additions and 6 deletions.
7 changes: 7 additions & 0 deletions bitmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (b *Bitmask) Len() uint64 {
return l
}

// Empty - return true if bitmask is entirely empty
func (b *Bitmask) Empty() bool {
b.mutex.RLock()
defer b.mutex.RUnlock()
return len(b.I) == 0
}

// NewBitMask - constructor
func NewBitMask() *Bitmask {
return &Bitmask{
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.
55 changes: 49 additions & 6 deletions pbf2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,25 @@ func main() {
log.Fatal(err)
}

// index interesting IDs in bitmasks
// index target IDs in bitmasks
index(idxDecoder, masks, config)

// rewind file
file.Seek(0, 0)
// no-op if no relation members of type 'way' present in mask
if !masks.RelWays.Empty() {
// === potential second pass (indexing) to index members of relations ===
file.Seek(io.SeekStart, 0) // rewind file
idxRelationsDecoder := osmpbf.NewDecoder(file)
err = idxRelationsDecoder.Start(runtime.GOMAXPROCS(-1)) // use several goroutines for faster decoding
if err != nil {
log.Fatal(err)
}

// === second pass (printing json) ===
// index relation member IDs in bitmasks
indexRelationMembers(idxRelationsDecoder, masks, config)
}

// === final pass (printing json) ===
file.Seek(io.SeekStart, 0) // rewind file
decoder := osmpbf.NewDecoder(file)
err = decoder.Start(runtime.GOMAXPROCS(-1)) // use several goroutines for faster decoding
if err != nil {
Expand Down Expand Up @@ -123,8 +135,8 @@ func index(d *osmpbf.Decoder, masks *BitmaskMap, config settings) {
case *osmpbf.Way:
if hasTags(v.Tags) && containsValidTags(v.Tags, config.Tags) {
masks.Ways.Insert(v.ID)
for _, wayid := range v.NodeIDs {
masks.WayRefs.Insert(wayid)
for _, nodeid := range v.NodeIDs {
masks.WayRefs.Insert(nodeid)
}
}

Expand All @@ -147,6 +159,37 @@ func index(d *osmpbf.Decoder, masks *BitmaskMap, config settings) {
}
}

func indexRelationMembers(d *osmpbf.Decoder, masks *BitmaskMap, config settings) {
for {
if v, err := d.Decode(); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
} else {
switch v := v.(type) {
case *osmpbf.Way:
if masks.RelWays.Has(v.ID) {
for _, nodeid := range v.NodeIDs {
masks.RelNodes.Insert(nodeid)
}
}
// support for super-relations
// case *osmpbf.Relation:
// if masks.RelRelation.Has(v.ID) {
// for _, member := range v.Members {
// switch member.Type {
// case 0: // node
// masks.RelNodes.Insert(member.ID)
// case 1: // way
// masks.RelWays.Insert(member.ID)
// }
// }
// }
}
}
}
}

func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings) {

batch := new(leveldb.Batch)
Expand Down
144 changes: 144 additions & 0 deletions test/fixtures/colon.json
Original file line number Diff line number Diff line change
Expand Up @@ -30282,5 +30282,149 @@
"w": "-123.1206628"
},
"gid": "way:340564714"
},
"relation:1414902": {
"id": 1414902,
"type": "relation",
"tags": {
"addr:housenumber": "4004",
"addr:street": "Wesbrook Mall",
"building": "yes",
"name": "TRIUMF MOB",
"source": "UBC Plant Ops 2011",
"type": "multipolygon",
"ubc:building_num": "853"
},
"centroid": {
"lat": "49.2476482",
"lon": "-123.2307034"
},
"bounds": {
"e": "-123.2301584",
"n": "49.2478749",
"s": "49.2473937",
"w": "-123.2309654"
},
"gid": "relation:1414902"
},
"relation:1671051": {
"id": 1671051,
"type": "relation",
"tags": {
"addr:housenumber": "2075",
"addr:street": "Kingsway",
"building": "yes",
"name": "Days Inn - Vancouver Metro",
"tourism": "motel",
"type": "multipolygon",
"website": "http://www.daysinnvancouvermetro.com/"
},
"centroid": {
"lat": "49.2449135",
"lon": "-123.0636847"
},
"bounds": {
"e": "-123.0631808",
"n": "49.2453123",
"s": "49.2446708",
"w": "-123.0640355"
},
"gid": "relation:1671051"
},
"relation:3274518": {
"id": 3274518,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "2146",
"addr:postcode": "V6T 1Z3",
"addr:street": "Health Sciences Mall",
"building": "yes",
"building:levels": "4",
"name": "D.H. Copp Building",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2655386",
"lon": "-123.2474331"
},
"bounds": {
"e": "-123.2467844",
"n": "49.2657471",
"s": "49.2652481",
"w": "-123.2482981"
},
"gid": "relation:3274518"
},
"relation:3579391": {
"id": 3579391,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "350",
"addr:street": "Nanaimo Street",
"building": "yes",
"cuisine": "sushi",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2814858",
"lon": "-123.0561845"
},
"bounds": {
"e": "-123.0560608",
"n": "49.2815786",
"s": "49.2813929",
"w": "-123.0563082"
},
"gid": "relation:3579391"
},
"relation:4055271": {
"id": 4055271,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "1950",
"addr:postcode": "V5L 1T7",
"addr:street": "East Pender Street",
"building": "school",
"name": "Sir William Macdonald Elementary School",
"phone": "+1 604 713 4696",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2806553",
"lon": "-123.0647792"
},
"bounds": {
"e": "-123.0643952",
"n": "49.2807812",
"s": "49.2804302",
"w": "-123.0649937"
},
"gid": "relation:4055271"
},
"relation:4600827": {
"id": 4600827,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "1856",
"addr:province": "BC",
"addr:street": "Frances Street",
"building": "residential",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2789797",
"lon": "-123.0671473"
},
"bounds": {
"e": "-123.0667246",
"n": "49.2791734",
"s": "49.2789588",
"w": "-123.0672027"
},
"gid": "relation:4600827"
}
}
144 changes: 144 additions & 0 deletions test/fixtures/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -29659,5 +29659,149 @@
"w": "-123.1206628"
},
"gid": "way:340564714"
},
"relation:1414902": {
"id": 1414902,
"type": "relation",
"tags": {
"addr:housenumber": "4004",
"addr:street": "Wesbrook Mall",
"building": "yes",
"name": "TRIUMF MOB",
"source": "UBC Plant Ops 2011",
"type": "multipolygon",
"ubc:building_num": "853"
},
"centroid": {
"lat": "49.2476482",
"lon": "-123.2307034"
},
"bounds": {
"e": "-123.2301584",
"n": "49.2478749",
"s": "49.2473937",
"w": "-123.2309654"
},
"gid": "relation:1414902"
},
"relation:1671051": {
"id": 1671051,
"type": "relation",
"tags": {
"addr:housenumber": "2075",
"addr:street": "Kingsway",
"building": "yes",
"name": "Days Inn - Vancouver Metro",
"tourism": "motel",
"type": "multipolygon",
"website": "http://www.daysinnvancouvermetro.com/"
},
"centroid": {
"lat": "49.2449135",
"lon": "-123.0636847"
},
"bounds": {
"e": "-123.0631808",
"n": "49.2453123",
"s": "49.2446708",
"w": "-123.0640355"
},
"gid": "relation:1671051"
},
"relation:3274518": {
"id": 3274518,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "2146",
"addr:postcode": "V6T 1Z3",
"addr:street": "Health Sciences Mall",
"building": "yes",
"building:levels": "4",
"name": "D.H. Copp Building",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2655386",
"lon": "-123.2474331"
},
"bounds": {
"e": "-123.2467844",
"n": "49.2657471",
"s": "49.2652481",
"w": "-123.2482981"
},
"gid": "relation:3274518"
},
"relation:3579391": {
"id": 3579391,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "350",
"addr:street": "Nanaimo Street",
"building": "yes",
"cuisine": "sushi",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2814858",
"lon": "-123.0561845"
},
"bounds": {
"e": "-123.0560608",
"n": "49.2815786",
"s": "49.2813929",
"w": "-123.0563082"
},
"gid": "relation:3579391"
},
"relation:4055271": {
"id": 4055271,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "1950",
"addr:postcode": "V5L 1T7",
"addr:street": "East Pender Street",
"building": "school",
"name": "Sir William Macdonald Elementary School",
"phone": "+1 604 713 4696",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2806553",
"lon": "-123.0647792"
},
"bounds": {
"e": "-123.0643952",
"n": "49.2807812",
"s": "49.2804302",
"w": "-123.0649937"
},
"gid": "relation:4055271"
},
"relation:4600827": {
"id": 4600827,
"type": "relation",
"tags": {
"addr:city": "Vancouver",
"addr:housenumber": "1856",
"addr:province": "BC",
"addr:street": "Frances Street",
"building": "residential",
"type": "multipolygon"
},
"centroid": {
"lat": "49.2789797",
"lon": "-123.0671473"
},
"bounds": {
"e": "-123.0667246",
"n": "49.2791734",
"s": "49.2789588",
"w": "-123.0672027"
},
"gid": "relation:4600827"
}
}
Loading

0 comments on commit 4445719

Please sign in to comment.