forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ogr_gmt.py
executable file
·237 lines (160 loc) · 7.74 KB
/
ogr_gmt.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
226
227
228
229
230
231
232
233
234
#!/usr/bin/env pytest
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: Test OGR GMT driver functionality.
# Author: Frank Warmerdam <[email protected]>
#
###############################################################################
# Copyright (c) 2007, Frank Warmerdam <[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 gdaltest
import ogrtest
from osgeo import gdal
from osgeo import ogr
###############################################################################
# Open Memory datasource.
def test_ogr_gmt_1():
gmt_drv = ogr.GetDriverByName('GMT')
gdaltest.gmt_ds = gmt_drv.CreateDataSource('tmp/tpoly.gmt')
assert gdaltest.gmt_ds is not None
###############################################################################
# Create table from data/poly.shp
def test_ogr_gmt_2():
#######################################################
# Create gmtory Layer
gdaltest.gmt_lyr = gdaltest.gmt_ds.CreateLayer('tpoly')
#######################################################
# Setup Schema
ogrtest.quick_create_layer_def(gdaltest.gmt_lyr,
[('AREA', ogr.OFTReal),
('EAS_ID', ogr.OFTInteger),
('PRFEDEA', ogr.OFTString)])
#######################################################
# Copy in poly.shp
dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn())
shp_ds = ogr.Open('data/poly.shp')
gdaltest.shp_ds = shp_ds
shp_lyr = shp_ds.GetLayer(0)
feat = shp_lyr.GetNextFeature()
gdaltest.poly_feat = []
while feat is not None:
gdaltest.poly_feat.append(feat)
dst_feat.SetFrom(feat)
gdaltest.gmt_lyr.CreateFeature(dst_feat)
feat = shp_lyr.GetNextFeature()
gdaltest.gmt_lyr = None
gdaltest.gmt_ds = None
###############################################################################
# Verify that stuff we just wrote is still OK.
def test_ogr_gmt_3():
gdaltest.gmt_ds = ogr.Open('tmp/tpoly.gmt')
gdaltest.gmt_lyr = gdaltest.gmt_ds.GetLayer(0)
expect = [168, 169, 166, 158, 165]
gdaltest.gmt_lyr.SetAttributeFilter('eas_id < 170')
tr = ogrtest.check_features_against_list(gdaltest.gmt_lyr,
'eas_id', expect)
gdaltest.gmt_lyr.SetAttributeFilter(None)
for i in range(len(gdaltest.poly_feat)):
orig_feat = gdaltest.poly_feat[i]
read_feat = gdaltest.gmt_lyr.GetNextFeature()
assert (ogrtest.check_feature_geometry(read_feat, orig_feat.GetGeometryRef(),
max_error=0.000000001) == 0)
for fld in range(3):
assert orig_feat.GetField(fld) == read_feat.GetField(fld), \
('Attribute %d does not match' % fld)
gdaltest.poly_feat = None
gdaltest.shp_ds = None
gdaltest.gmt_lyr = None
gdaltest.gmt_ds = None
assert tr
###############################################################################
# Verify reading of multilinestring file. (#3802)
def test_ogr_gmt_4():
ds = ogr.Open('data/test_multi.gmt')
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetGeomType() == ogr.wkbMultiLineString, \
'did not get expected multilinestring type.'
feat = lyr.GetNextFeature()
assert not ogrtest.check_feature_geometry(feat, 'MULTILINESTRING ((175 -45,176 -45),(180.0 -45.3,179.0 -45.4))')
assert feat.GetField('name') == 'feature 1', 'got wrong name, feature 1'
feat = lyr.GetNextFeature()
assert not ogrtest.check_feature_geometry(feat, 'MULTILINESTRING ((175.1 -45.0,175.2 -45.1),(180.1 -45.3,180.0 -45.2))')
assert feat.GetField('name') == 'feature 2', 'got wrong name, feature 2'
feat = lyr.GetNextFeature()
assert feat is None, 'did not get null feature when expected.'
###############################################################################
# Write a multipolygon file and verify it.
def test_ogr_gmt_5():
#######################################################
# Create gmtory Layer
gmt_drv = ogr.GetDriverByName('GMT')
gdaltest.gmt_ds = gmt_drv.CreateDataSource('tmp/mpoly.gmt')
gdaltest.gmt_lyr = gdaltest.gmt_ds.CreateLayer('mpoly')
#######################################################
# Setup Schema
ogrtest.quick_create_layer_def(gdaltest.gmt_lyr,
[('ID', ogr.OFTInteger)])
#######################################################
# Write a first multipolygon
dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn())
dst_feat.SetGeometryDirectly(
ogr.CreateGeometryFromWkt('MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))'))
dst_feat.SetField('ID', 15)
gdal.SetConfigOption('GMT_USE_TAB', 'TRUE') # Ticket #6453
gdaltest.gmt_lyr.CreateFeature(dst_feat)
gdal.SetConfigOption('GMT_USE_TAB', None)
dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn())
dst_feat.SetGeometryDirectly(
ogr.CreateGeometryFromWkt('MULTIPOLYGON(((30 20,40 20,30 30,30 20)))'))
dst_feat.SetField('ID', 16)
gdaltest.gmt_lyr.CreateFeature(dst_feat)
gdaltest.gmt_lyr = None
gdaltest.gmt_ds = None
# Reopen.
ds = ogr.Open('tmp/mpoly.gmt')
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetGeomType() == ogr.wkbMultiPolygon, \
'did not get expected multipolygon type.'
feat = lyr.GetNextFeature()
assert not ogrtest.check_feature_geometry(feat, 'MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))')
assert feat.GetField('ID') == 15, 'got wrong id, first feature'
feat = lyr.GetNextFeature()
assert not ogrtest.check_feature_geometry(feat, 'MULTIPOLYGON(((30 20,40 20,30 30,30 20)))')
assert feat.GetField('ID') == 16, 'got wrong ID, second feature'
feat = lyr.GetNextFeature()
assert feat is None, 'did not get null feature when expected.'
###############################################################################
# Test reading a file with just point coordinates
def test_ogr_gmt_coord_only():
with gdaltest.tempfile('/vsimem/test.gmt', """1 2 3\n"""):
ds = ogr.Open('/vsimem/test.gmt')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert not ogrtest.check_feature_geometry(f, 'POINT Z (1 2 3)'), f.GetGeometryRef().ExportToIsoWkt()
###############################################################################
#
def test_ogr_gmt_cleanup():
if gdaltest.gmt_ds is not None:
gdaltest.gmt_lyr = None
gdaltest.gmt_ds = None
gdaltest.clean_tmp()