Ejemplo n.º 1
0
func makeWay(id int64, tags element.Tags, coords []coord) element.Way {
	way := element.Way{}
	way.Id = id
	way.Tags = tags
	for _, coord := range coords {
		way.Refs = append(way.Refs, coord.id)
		way.Nodes = append(way.Nodes,
			element.Node{OSMElem: element.OSMElem{Id: coord.id}, Long: coord.long, Lat: coord.lat})
	}
	return way
}
Ejemplo n.º 2
0
func TestRingMerge(t *testing.T) {
	w1 := element.Way{}
	w1.Id = 1
	w1.Refs = []int64{1, 2, 3}
	w1.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r1 := newRing(&w1)

	w2 := element.Way{}
	w2.Id = 2
	w2.Refs = []int64{3, 4, 1}
	w2.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r2 := newRing(&w2)
	rings := []*ring{r1, r2}

	result := mergeRings(rings)
	if len(result) != 1 {
		t.Fatal(result)
	}
	r := result[0]
	expected := []int64{1, 2, 3, 4, 1}
	for i, ref := range r.refs {
		if ref != expected[i] {
			t.Fatalf("%v != %v", r.refs, expected)
		}
	}
}
Ejemplo n.º 3
0
func TestRingMergeMissingRefs(t *testing.T) {
	// way without refs should not panic with index out of range
	w1 := element.Way{}
	w1.Id = 1
	w1.Refs = []int64{1, 2, 3}
	w1.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r1 := newRing(&w1)

	w2 := element.Way{}
	w2.Id = 2
	w2.Refs = []int64{}
	w2.Nodes = []element.Node{}
	r2 := newRing(&w2)
	rings := []*ring{r1, r2}

	result := mergeRings(rings)
	if len(result) != 1 {
		t.Fatal(result)
	}
	if result[0] != r1 {
		t.Fatal(result[0])
	}
}
Ejemplo n.º 4
0
func TestLineStringMatcher(t *testing.T) {
	elem := element.Way{}
	ls := mapping.LineStringMatcher()

	elem.Tags = element.Tags{"unknown": "baz"}
	matchesEqual(t, []Match{}, ls.MatchWay(&elem))

	elem.Tags = element.Tags{"highway": "unknown"}
	matchesEqual(t, []Match{}, ls.MatchWay(&elem))

	elem.Tags = element.Tags{"highway": "pedestrian"}
	matchesEqual(t, []Match{{"highway", "pedestrian", DestTable{"roads", "roads"}, nil}}, ls.MatchWay(&elem))

	// exclude_tags area=yes
	elem.Tags = element.Tags{"highway": "pedestrian", "area": "yes"}
	matchesEqual(t, []Match{}, ls.MatchWay(&elem))

	elem.Tags = element.Tags{"highway": "secondary", "railway": "tram"}
	matchesEqual(t,
		[]Match{
			{"highway", "secondary", DestTable{"roads", "roads"}, nil},
			{"railway", "tram", DestTable{"roads", "railway"}, nil}},
		ls.MatchWay(&elem))

	elem.Tags = element.Tags{"highway": "footway", "landuse": "park"}
	// landusages not a linestring table
	matchesEqual(t, []Match{{"highway", "footway", DestTable{"roads", "roads"}, nil}}, ls.MatchWay(&elem))
}
Ejemplo n.º 5
0
func (tm *tagMatcher) MatchWay(way *element.Way) []Match {
	if tm.matchAreas { // match way as polygon
		if way.IsClosed() {
			if way.Tags["area"] == "no" {
				return nil
			}
			return tm.match(&way.Tags)
		}
	} else { // match way as linestring
		if way.IsClosed() {
			if way.Tags["area"] == "yes" {
				return nil
			}
		}
		return tm.match(&way.Tags)
	}
	return nil
}
Ejemplo n.º 6
0
func (self *DeltaCoordsCache) FillWay(way *element.Way) error {
	if way == nil {
		return nil
	}
	way.Nodes = make([]element.Node, len(way.Refs))

	var err error
	var bunch *coordsBunch
	var bunchId, lastBunchId int64
	lastBunchId = -1

	for i, id := range way.Refs {
		bunchId = self.getBunchId(id)
		// re-use bunches
		if bunchId != lastBunchId {
			if bunch != nil {
				bunch.Unlock()
			}
			bunch, err = self.getBunch(bunchId)
			if err != nil {
				return err
			}
		}
		lastBunchId = bunchId

		nd, err := bunch.GetCoord(id)
		if err != nil {
			bunch.Unlock()
			return err
		}
		way.Nodes[i] = *nd
	}
	if bunch != nil {
		bunch.Unlock()
	}
	return nil
}
Ejemplo n.º 7
0
func TestDiffCache(t *testing.T) {

	cache_dir, _ := ioutil.TempDir("", "imposm3_test")
	defer os.RemoveAll(cache_dir)

	cache, err := newCoordsRefIndex(cache_dir)
	if err != nil {
		t.Fatal()
	}
	defer cache.Close()

	w1 := element.Way{}
	w1.Id = 100
	w1.Nodes = []element.Node{
		{OSMElem: element.OSMElem{Id: 1000}},
		{OSMElem: element.OSMElem{Id: 1001}},
		{OSMElem: element.OSMElem{Id: 1002}},
	}
	cache.AddFromWay(&w1)

	w2 := element.Way{}
	w2.Id = 200
	w2.Nodes = []element.Node{
		{OSMElem: element.OSMElem{Id: 1002}},
		{OSMElem: element.OSMElem{Id: 1003}},
		{OSMElem: element.OSMElem{Id: 1004}},
	}
	cache.AddFromWay(&w2)

	cache.DeleteFromWay(&w1)

	if ids := cache.Get(1000); len(ids) != 0 {
		t.Fatal(ids)
	}
	if ids := cache.Get(1002); len(ids) != 1 {
		t.Fatal(ids)
	}

}
Ejemplo n.º 8
0
func TestRingMergeReverseEndpoints(t *testing.T) {
	w1 := element.Way{}
	w1.Id = 1
	w1.Refs = []int64{1, 2, 3, 4}
	w1.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r1 := newRing(&w1)

	w2 := element.Way{}
	w2.Id = 2
	w2.Refs = []int64{6, 5, 4}
	w2.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r2 := newRing(&w2)

	w3 := element.Way{}
	w3.Id = 3
	w3.Refs = []int64{1, 7, 6}
	w3.Nodes = []element.Node{
		element.Node{},
		element.Node{},
		element.Node{},
	}
	r3 := newRing(&w3)

	rings := []*ring{r1, r2, r3}

	result := mergeRings(rings)
	if len(result) != 1 {
		t.Fatal(result)
	}
	r := result[0]
	expected := []int64{6, 5, 4, 3, 2, 1, 7, 6}
	for i, ref := range r.refs {
		if ref != expected[i] {
			t.Fatalf("%v != %v", r.refs, expected)
		}
	}
}
Ejemplo n.º 9
0
func TestRingMergePermutations(t *testing.T) {
	// Test all possible permutations of 4 ring segments.
	for i := 0; i < 16; i++ {
		// test each segment in both directions
		f1 := i&1 == 0
		f2 := i&2 == 0
		f3 := i&4 == 0
		f4 := i&8 == 0

		indices := []int{0, 1, 2, 3}

		for permutationFirst(sort.IntSlice(indices)); permutationNext(sort.IntSlice(indices)); {
			ways := make([][]int64, 4)
			if f1 {
				ways[0] = []int64{1, 2, 3, 4}
			} else {
				ways[0] = []int64{4, 3, 2, 1}
			}
			if f2 {
				ways[1] = []int64{4, 5, 6, 7}
			} else {
				ways[1] = []int64{7, 6, 5, 4}
			}
			if f3 {
				ways[2] = []int64{7, 8, 9, 10}
			} else {
				ways[2] = []int64{10, 9, 8, 7}
			}
			if f4 {
				ways[3] = []int64{10, 11, 12, 1}
			} else {
				ways[3] = []int64{1, 12, 11, 10}
			}

			w1 := element.Way{}
			w1.Id = 1
			w1.Refs = ways[indices[0]]
			w1.Nodes = []element.Node{element.Node{}, element.Node{}, element.Node{}, element.Node{}}
			w2 := element.Way{}
			w2.Id = 2
			w2.Refs = ways[indices[1]]
			w2.Nodes = []element.Node{element.Node{}, element.Node{}, element.Node{}, element.Node{}}
			w3 := element.Way{}
			w3.Id = 3
			w3.Refs = ways[indices[2]]
			w3.Nodes = []element.Node{element.Node{}, element.Node{}, element.Node{}, element.Node{}}
			w4 := element.Way{}
			w4.Id = 4
			w4.Refs = ways[indices[3]]
			w4.Nodes = []element.Node{element.Node{}, element.Node{}, element.Node{}, element.Node{}}

			rings := []*ring{
				&ring{ways: []*element.Way{&w1}, refs: w1.Refs, nodes: w1.Nodes},
				&ring{ways: []*element.Way{&w2}, refs: w2.Refs, nodes: w2.Nodes},
				&ring{ways: []*element.Way{&w3}, refs: w3.Refs, nodes: w3.Nodes},
				&ring{ways: []*element.Way{&w4}, refs: w4.Refs, nodes: w4.Nodes},
			}
			result := mergeRings(rings)
			if len(result) != 1 {
				t.Fatalf("not a single ring: %v\n", result)
			}

			r := result[0].refs

			if r[0] != r[len(r)-1] {
				t.Fatalf("ring not closed: %v", r)
			}

			asc := true
			desc := true

			for i := 1; i < len(r); i++ {
				if r[i] == 1 || r[i-1] < r[i] {
					continue
				} else {
					asc = false
					break
				}
			}
			for i := 1; i < len(r); i++ {
				if r[i] == 12 || r[i-1] > r[i] {
					continue
				} else {
					desc = false
					break
				}
			}

			if !(asc || desc) {
				t.Fatalf("ring not ascending/descending: %v, asc: %v, desc: %v", r, asc, desc)
			}
		}
	}
}