forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnm_test.py
executable file
·227 lines (159 loc) · 6.36 KB
/
gnm_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env pytest
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: Test basic GNMGdalNetwork class functionality.
# Authors: Mikhail Gusev (gusevmihs at gmail dot com)
# Dmitry Baryshnikov, [email protected]
#
###############################################################################
# Copyright (c) 2014, Mikhail Gusev
# Copyright (c) 2014-2015, NextGIS <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import os
import shutil
from osgeo import gdal
from osgeo import gnm
import pytest
###############################################################################
# Create file base network
def test_gnm_filenetwork_create():
try:
shutil.rmtree('tmp/test_gnm')
except OSError:
pass
ogrtest.drv = None
ogrtest.have_gnm = 0
ogrtest.drv = gdal.GetDriverByName('GNMFile')
if ogrtest.drv is None:
pytest.skip()
ds = ogrtest.drv.Create('tmp/', 0, 0, 0, gdal.GDT_Unknown, options=['net_name=test_gnm', 'net_description=Test file based GNM', 'net_srs=EPSG:4326'])
# cast to GNM
dn = gnm.CastToNetwork(ds)
assert dn is not None
assert dn.GetVersion() == 100, 'GNM: Check GNM version failed'
assert dn.GetName() == 'test_gnm', 'GNM: Check GNM name failed'
assert dn.GetDescription() == 'Test file based GNM', \
'GNM: Check GNM description failed'
dn = None
ogrtest.have_gnm = 1
###############################################################################
# Open file base network
def test_gnm_filenetwork_open():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
# cast to GNM
dn = gnm.CastToNetwork(ds)
assert dn is not None
assert dn.GetVersion() == 100, 'GNM: Check GNM version failed'
assert dn.GetName() == 'test_gnm', 'GNM: Check GNM name failed'
assert dn.GetDescription() == 'Test file based GNM', \
'GNM: Check GNM description failed'
dn = None
###############################################################################
# Import layers into file base network
def test_gnm_import():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
# pipes
dspipes = gdal.OpenEx('data/pipes.shp', gdal.OF_VECTOR)
lyrpipes = dspipes.GetLayerByIndex(0)
new_lyr = ds.CopyLayer(lyrpipes, 'pipes')
assert new_lyr is not None, 'failed to import pipes'
dspipes = None
new_lyr = None
# wells
dswells = gdal.OpenEx('data/wells.shp', gdal.OF_VECTOR)
lyrwells = dswells.GetLayerByIndex(0)
new_lyr = ds.CopyLayer(lyrwells, 'wells')
assert new_lyr is not None, 'failed to import wells'
dswells = None
new_lyr = None
assert ds.GetLayerCount() == 2, 'expected 2 layers'
ds = None
###############################################################################
# autoconnect
def test_gnm_autoconnect():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
dgn = gnm.CastToGenericNetwork(ds)
assert dgn is not None, 'cast to GNMGenericNetwork failed'
ret = dgn.ConnectPointsByLines(['pipes', 'wells'], 0.000001, 1, 1, gnm.GNM_EDGE_DIR_BOTH)
assert ret == 0, 'failed to connect'
dgn = None
###############################################################################
# Dijkstra shortest path
def test_gnm_graph_dijkstra():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
dn = gnm.CastToNetwork(ds)
assert dn is not None, 'cast to GNMNetwork failed'
lyr = dn.GetPath(61, 50, gnm.GATDijkstraShortestPath)
assert lyr is not None, 'failed to get path'
if lyr.GetFeatureCount() == 0:
dn.ReleaseResultSet(lyr)
pytest.fail('failed to get path')
dn.ReleaseResultSet(lyr)
dn = None
import ogrtest
###############################################################################
# KShortest Paths
def test_gnm_graph_kshortest():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
dn = gnm.CastToNetwork(ds)
assert dn is not None, 'cast to GNMNetwork failed'
lyr = dn.GetPath(61, 50, gnm.GATKShortestPath, options=['num_paths=3'])
assert lyr is not None, 'failed to get path'
if lyr.GetFeatureCount() < 20:
dn.ReleaseResultSet(lyr)
pytest.fail('failed to get path')
dn.ReleaseResultSet(lyr)
dn = None
###############################################################################
# ConnectedComponents
def test_gnm_graph_connectedcomponents():
if not ogrtest.have_gnm:
pytest.skip()
ds = gdal.OpenEx('tmp/test_gnm')
dn = gnm.CastToNetwork(ds)
assert dn is not None, 'cast to GNMNetwork failed'
lyr = dn.GetPath(61, 50, gnm.GATConnectedComponents)
assert lyr is not None, 'failed to get path'
if lyr.GetFeatureCount() == 0:
dn.ReleaseResultSet(lyr)
pytest.fail('failed to get path')
dn.ReleaseResultSet(lyr)
dn = None
###############################################################################
# Network deleting
def test_gnm_delete():
if not ogrtest.have_gnm:
pytest.skip()
gdal.GetDriverByName('GNMFile').Delete('tmp/test_gnm')
assert not os.path.exists('tmp/test_gnm')