Skip to content

Commit 6792131

Browse files
committed
Add test for pointOnFeature
1 parent b0d15a5 commit 6792131

File tree

1 file changed

+156
-4
lines changed

1 file changed

+156
-4
lines changed

measurement/measurement_test.go

+156-4
Original file line numberDiff line numberDiff line change
@@ -1608,9 +1608,10 @@ func TestInBBox(t *testing.T) {
16081608

16091609
func TestInRing(t *testing.T) {
16101610
tests := map[string]struct {
1611-
point geometry.Point
1612-
ring []geometry.Point
1613-
want bool
1611+
point geometry.Point
1612+
ring []geometry.Point
1613+
ignoreBoundary bool
1614+
want bool
16141615
} {
16151616
"point in ring": {
16161617
point: geometry.Point{
@@ -1639,6 +1640,7 @@ func TestInRing(t *testing.T) {
16391640
Lat: 0.0,
16401641
},
16411642
},
1643+
ignoreBoundary: false,
16421644
want: true,
16431645
},
16441646
"point not in ring": {
@@ -1668,13 +1670,74 @@ func TestInRing(t *testing.T) {
16681670
Lat: 0.0,
16691671
},
16701672
},
1673+
ignoreBoundary: false,
1674+
want: false,
1675+
},
1676+
"point on ring": {
1677+
point: geometry.Point{
1678+
Lng: 0.0,
1679+
Lat: 0.0,
1680+
},
1681+
ring: []geometry.Point {
1682+
{
1683+
Lng: 0.0,
1684+
Lat: 0.0,
1685+
},
1686+
{
1687+
Lng: 0.0,
1688+
Lat: 100.0,
1689+
},
1690+
{
1691+
Lng: 100.0,
1692+
Lat: 100.0,
1693+
},
1694+
{
1695+
Lng: 100.0,
1696+
Lat: 0.0,
1697+
},
1698+
{
1699+
Lng: 0.0,
1700+
Lat: 0.0,
1701+
},
1702+
},
1703+
ignoreBoundary: false,
1704+
want: true,
1705+
},
1706+
"point on ring, ignore boundary": {
1707+
point: geometry.Point{
1708+
Lng: 0.0,
1709+
Lat: 0.0,
1710+
},
1711+
ring: []geometry.Point {
1712+
{
1713+
Lng: 0.0,
1714+
Lat: 0.0,
1715+
},
1716+
{
1717+
Lng: 0.0,
1718+
Lat: 100.0,
1719+
},
1720+
{
1721+
Lng: 100.0,
1722+
Lat: 100.0,
1723+
},
1724+
{
1725+
Lng: 100.0,
1726+
Lat: 0.0,
1727+
},
1728+
{
1729+
Lng: 0.0,
1730+
Lat: 0.0,
1731+
},
1732+
},
1733+
ignoreBoundary: true,
16711734
want: false,
16721735
},
16731736
}
16741737

16751738
for name, tt := range tests {
16761739
t.Run(name, func(t *testing.T) {
1677-
assert.Equal(t, inRing(tt.point, tt.ring, false), tt.want)
1740+
assert.Equal(t, inRing(tt.point, tt.ring, tt.ignoreBoundary), tt.want)
16781741
})
16791742
}
16801743
}
@@ -1728,3 +1791,92 @@ func TestInPolygon(t *testing.T) {
17281791
})
17291792
}
17301793
}
1794+
1795+
1796+
func TestPointOnFeature(t *testing.T) {
1797+
fs, err := createFeature(t, AreaPolygon)
1798+
if err != nil {
1799+
t.Errorf("createFeature error = %v", err)
1800+
}
1801+
1802+
p, err := PointOnFeature(*fs, nil, "")
1803+
if err != nil {
1804+
t.Errorf("PointOnFeature error = %v", err)
1805+
} else {
1806+
if p == nil {
1807+
t.Error("point cannot be empty")
1808+
}
1809+
if p != nil {
1810+
assert.Equal(t, p.Lng, 133.0)
1811+
assert.Equal(t, p.Lat, -26.857142857142858)
1812+
}
1813+
return
1814+
}
1815+
}
1816+
1817+
func TestPointOnFeatureOfImbalancedPolygon(t *testing.T) {
1818+
fs, err := createFeature(t, ImbalancedPolygon)
1819+
if err != nil {
1820+
t.Errorf("createFeature error = %v", err)
1821+
}
1822+
1823+
p, err := PointOnFeature(*fs, nil, "")
1824+
if err != nil {
1825+
t.Errorf("PointOnFeature error = %v", err)
1826+
} else {
1827+
if p == nil {
1828+
t.Error("point cannot be empty")
1829+
}
1830+
if p != nil {
1831+
assert.Equal(t, p.Lng, 4.851791984156558)
1832+
assert.Equal(t, p.Lat, 45.78143055383553)
1833+
}
1834+
return
1835+
}
1836+
}
1837+
1838+
func TestPointOnFeatureCollection(t *testing.T) {
1839+
fc, err := createFeatureCollection(t, AreaFeatureCollection)
1840+
if err != nil {
1841+
t.Errorf("createFeatureCollection error: %v", err)
1842+
}
1843+
1844+
p, err := PointOnFeatureCollection(*fc, nil, "")
1845+
if err != nil {
1846+
t.Errorf("PointOnFeature error = %v", err)
1847+
} else {
1848+
if p == nil {
1849+
t.Error("point cannot be empty")
1850+
}
1851+
if p != nil {
1852+
assert.Equal(t, p.Lng, 6.774169921875)
1853+
assert.Equal(t, p.Lat, 47.486422855836416)
1854+
}
1855+
return
1856+
}
1857+
}
1858+
1859+
1860+
func createFeature(t *testing.T, geometryString string) (*feature.Feature, error) {
1861+
gjson, err := utils.LoadJSONFixture(geometryString)
1862+
if err != nil {
1863+
t.Errorf("LoadJSONFixture error: %v", err)
1864+
}
1865+
fs, err := feature.FromJSON(gjson)
1866+
if err != nil {
1867+
t.Errorf("FromJSON error: %v", err)
1868+
}
1869+
return fs, err
1870+
}
1871+
1872+
func createFeatureCollection(t *testing.T, geometryString string) (*feature.Collection, error) {
1873+
gjson, err := utils.LoadJSONFixture(geometryString)
1874+
if err != nil {
1875+
t.Errorf("LoadJSONFixture error: %v", err)
1876+
}
1877+
fs, err := feature.CollectionFromJSON(gjson)
1878+
if err != nil {
1879+
t.Errorf("FromJSON error: %v", err)
1880+
}
1881+
return fs, err
1882+
}

0 commit comments

Comments
 (0)