Skip to content

Commit 44ea5a8

Browse files
authored
Issue207 (#208)
* do not load all requests to memory when running dump * refactor wfs module, remove out_file option from dump, add standard options to cat * standardize cat and dump
1 parent 0369f90 commit 44ea5a8

File tree

7 files changed

+156
-257
lines changed

7 files changed

+156
-257
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
0.14.0 (2024-12-17)
5+
------------------
6+
- simplify WFS module, standardize cat/dump options
7+
48
0.13.0 (2024-12-11)
59
------------------
610
- support Data Catalogue API changes (#188)

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ AERODROME_STATUS AIRCRAFT_ACCESS_IND AIRPORT_NAME
9191
```
9292

9393
### CLI
94-
Commands available via the bcdata command line interface are documented with the `--help` option:
94+
95+
Commands available via the bcdata command line interface are documented with the --help option
9596

9697
```
9798

@@ -104,7 +105,7 @@ Options:
104105
--help Show this message and exit.
105106

106107
Commands:
107-
bc2pg Download a DataBC WFS layer to postgres
108+
bc2pg Load a DataBC WFS layer to a postgres db
108109
cat Write DataBC features to stdout as GeoJSON feature objects.
109110
dem Dump BC DEM to TIFF
110111
dump Write DataBC features to stdout as GeoJSON feature collection.
@@ -119,7 +120,7 @@ $ bcdata bc2pg --help
119120

120121
Usage: bcdata bc2pg [OPTIONS] DATASET
121122

122-
Download a DataBC WFS layer to postgres
123+
Load a DataBC WFS layer to a postgres db
123124

124125
$ bcdata bc2pg whse_imagery_and_base_maps.gsr_airports_svw
125126

@@ -158,16 +159,18 @@ Usage: bcdata cat [OPTIONS] DATASET
158159

159160
Options:
160161
--query TEXT A valid CQL or ECQL query
162+
-c, --count INTEGER Number of features to request and dump
161163
--bounds TEXT Bounds: "left bottom right top" or "[left,
162164
bottom, right, top]". Coordinates are BC
163165
Albers (default) or --bounds_crs
166+
--bounds-crs, --bounds_crs TEXT
167+
CRS of provided bounds
164168
--indent INTEGER Indentation level for JSON output
165169
--compact / --not-compact Use compact separators (',', ':').
166170
--dst-crs, --dst_crs TEXT Destination CRS
167171
-s, --sortby TEXT Name of sort field
168-
--bounds-crs, --bounds_crs TEXT
169-
CRS of provided bounds
170172
-l, --lowercase Write column/properties names as lowercase
173+
-m, --promote-to-multi Promote features to multipart
171174
-v, --verbose Increase verbosity.
172175
-q, --quiet Decrease verbosity.
173176
--help Show this message and exit.
@@ -187,9 +190,8 @@ Options:
187190
--bounds TEXT Bounds: "left bottom right top" or "[left,
188191
bottom, right, top]". Coordinates are BC
189192
Albers (default) or --bounds_crs [required]
190-
--dst-crs, --dst_crs TEXT Destination CRS
191-
--bounds-crs, --bounds_crs TEXT
192-
CRS of provided bounds
193+
--dst-crs TEXT CRS of output file
194+
--bounds-crs TEXT CRS of provided bounds
193195
-r, --resolution INTEGER
194196
-a, --align Align provided bounds to provincial standard
195197
-i, --interpolation [nearest|bilinear|bicubic]
@@ -211,20 +213,20 @@ Usage: bcdata dump [OPTIONS] DATASET
211213
$ bcdata dump bc-airports --query "AIRPORT_NAME='Victoria Harbour (Shoal Point) Heliport'"
212214
$ bcdata dump bc-airports --bounds xmin ymin xmax ymax
213215

214-
It can also be combined to read bounds of a feature dataset using Fiona:
216+
It can also be combined to read bounds of a feature dataset using Fiona:
215217
$ bcdata dump bc-airports --bounds $(fio info aoi.shp --bounds)
216218

217219
Options:
218220
--query TEXT A valid CQL or ECQL query
219-
-o, --out_file TEXT Output file
220221
-c, --count INTEGER Number of features to request and dump
221222
--bounds TEXT Bounds: "left bottom right top" or "[left,
222223
bottom, right, top]". Coordinates are BC
223224
Albers (default) or --bounds_crs
224225
--bounds-crs, --bounds_crs TEXT
225226
CRS of provided bounds
226-
-nc, --no-clean Do not do any data standardization
227+
-s, --sortby TEXT Name of sort field
227228
-l, --lowercase Write column/properties names as lowercase
229+
-m, --promote-to-multi Promote features to multipart
228230
-v, --verbose Increase verbosity.
229231
-q, --quiet Decrease verbosity.
230232
--help Show this message and exit.
@@ -261,6 +263,7 @@ Usage: bcdata list [OPTIONS]
261263
List DataBC layers available via WFS
262264

263265
Options:
266+
-r, --refresh Refresh the cached list
264267
--help Show this message and exit.
265268
```
266269

src/bcdata/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
from .bcdc import get_table_definition as get_table_definition
55
from .bcdc import get_table_name as get_table_name
66
from .wcs import get_dem as get_dem
7-
from .wfs import define_requests as define_requests
87
from .wfs import get_count as get_count
98
from .wfs import get_data as get_data
10-
from .wfs import get_features as get_features
119
from .wfs import get_sortkey as get_sortkey
1210
from .wfs import list_tables as list_tables
1311
from .wfs import validate_name as validate_name
@@ -24,4 +22,4 @@
2422
raise Exception(f"Failed to download primary key database at {PRIMARY_KEY_DB_URL}")
2523
primary_keys = {}
2624

27-
__version__ = "0.14.0dev0"
25+
__version__ = "0.14.0"

src/bcdata/bc2pg.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,8 @@ def bc2pg( # noqa: C901
7373
WFS = BCWFS()
7474

7575
# define requests
76-
urls = bcdata.define_requests(
77-
dataset,
78-
query=query,
79-
bounds=bounds,
80-
bounds_crs=bounds_crs,
81-
count=count,
82-
sortby=sortby,
83-
crs="epsg:3005",
76+
urls = WFS.define_requests(
77+
dataset, query=query, bounds=bounds, bounds_crs=bounds_crs, count=count, sortby=sortby
8478
)
8579

8680
df = None # just for tracking if first download is done by geometry type check
@@ -101,7 +95,7 @@ def bc2pg( # noqa: C901
10195

10296
# if geometry type is not provided, determine type by making the first request
10397
if not geometry_type:
104-
df = WFS.make_requests(dataset=dataset, urls=[urls[0]], as_gdf=True, crs="epsg:3005", lowercase=True)
98+
df = WFS.request_features(url=urls[0], as_gdf=True, crs="epsg:3005", lowercase=True)
10599
geometry_type = df.geom_type.unique()[0] # keep only the first type
106100
if numpy.any(df.has_z.unique()[0]): # geopandas does not include Z in geom_type string
107101
geometry_type = geometry_type + "Z"
@@ -110,9 +104,8 @@ def bc2pg( # noqa: C901
110104
# (in case all entrys with geom are near the bottom)
111105
if not geometry_type:
112106
if not urls[-1] == urls[0]:
113-
df_temp = WFS.make_requests(
114-
dataset=dataset,
115-
urls=[urls[-1]],
107+
df_temp = WFS.request_features(
108+
url=urls[-1],
116109
as_gdf=True,
117110
crs="epsg:3005",
118111
lowercase=True,
@@ -165,7 +158,7 @@ def bc2pg( # noqa: C901
165158
for n, url in enumerate(urls):
166159
# if first url not downloaded above when checking geom type, do now
167160
if df is None:
168-
df = WFS.make_requests(dataset=dataset, urls=[url], as_gdf=True, crs="epsg:3005", lowercase=True)
161+
df = WFS.request_features(url=url, as_gdf=True, crs="epsg:3005", lowercase=True)
169162
# tidy the resulting dataframe
170163
df = df.rename_geometry("geom")
171164
# lowercasify

src/bcdata/cli.py

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ def dem(
196196
"--query",
197197
help="A valid CQL or ECQL query",
198198
)
199-
@click.option("--out_file", "-o", help="Output file")
200199
@click.option(
201200
"--count",
202201
"-c",
@@ -211,17 +210,20 @@ def dem(
211210
help="CRS of provided bounds",
212211
default="EPSG:3005",
213212
)
213+
@click.option("--sortby", "-s", help="Name of sort field")
214+
@lowercase_opt
214215
@click.option(
215-
"--no-clean",
216-
"-nc",
217-
help="Do not do any data standardization",
216+
"--promote-to-multi",
217+
"-m",
218+
help="Promote features to multipart",
218219
is_flag=True,
219-
default=True,
220+
default=False,
220221
)
221-
@lowercase_opt
222222
@verbose_opt
223223
@quiet_opt
224-
def dump(dataset, query, out_file, count, bounds, bounds_crs, no_clean, lowercase, verbose, quiet):
224+
def dump(
225+
dataset, query, count, bounds, bounds_crs, sortby, lowercase, promote_to_multi, verbose, quiet
226+
):
225227
"""Write DataBC features to stdout as GeoJSON feature collection.
226228
227229
\b
@@ -237,25 +239,19 @@ def dump(dataset, query, out_file, count, bounds, bounds_crs, no_clean, lowercas
237239
verbosity = verbose - quiet
238240
configure_logging(verbosity)
239241
table = bcdata.validate_name(dataset)
240-
if no_clean:
241-
clean = False
242-
else:
243-
clean = True
244242
data = bcdata.get_data(
245243
table,
246244
query=query,
247245
count=count,
248246
bounds=bounds,
249247
bounds_crs=bounds_crs,
248+
sortby=sortby,
250249
lowercase=lowercase,
251-
clean=clean,
250+
promote_to_multi=promote_to_multi,
251+
as_gdf=False,
252252
)
253-
if out_file:
254-
with open(out_file, "w") as sink:
255-
sink.write(json.dumps(data))
256-
else:
257-
sink = click.get_text_stream("stdout")
258-
sink.write(json.dumps(data))
253+
sink = click.get_text_stream("stdout")
254+
sink.write(json.dumps(data))
259255

260256

261257
@cli.command()
@@ -264,30 +260,46 @@ def dump(dataset, query, out_file, count, bounds, bounds_crs, no_clean, lowercas
264260
"--query",
265261
help="A valid CQL or ECQL query",
266262
)
263+
@click.option(
264+
"--count",
265+
"-c",
266+
default=None,
267+
type=int,
268+
help="Number of features to request and dump",
269+
)
267270
@bounds_opt
268-
@indent_opt
269-
@compact_opt
270-
@dst_crs_opt
271-
@click.option("--sortby", "-s", help="Name of sort field")
272271
@click.option(
273272
"--bounds-crs",
274273
"--bounds_crs",
275274
help="CRS of provided bounds",
276275
default="EPSG:3005",
277276
)
277+
@indent_opt
278+
@compact_opt
279+
@dst_crs_opt
280+
@click.option("--sortby", "-s", help="Name of sort field")
278281
@lowercase_opt
282+
@click.option(
283+
"--promote-to-multi",
284+
"-m",
285+
help="Promote features to multipart",
286+
is_flag=True,
287+
default=False,
288+
)
279289
@verbose_opt
280290
@quiet_opt
281291
def cat(
282292
dataset,
283293
query,
294+
count,
284295
bounds,
285296
bounds_crs,
286297
indent,
287298
compact,
288299
dst_crs,
289300
sortby,
290301
lowercase,
302+
promote_to_multi,
291303
verbose,
292304
quiet,
293305
):
@@ -303,23 +315,23 @@ def cat(
303315
if compact:
304316
dump_kwds["separators"] = (",", ":")
305317
table = bcdata.validate_name(dataset)
306-
for feat in bcdata.get_features(
318+
WFS = bcdata.wfs.BCWFS()
319+
for url in WFS.define_requests(
307320
table,
308321
query=query,
322+
count=count,
309323
bounds=bounds,
310324
bounds_crs=bounds_crs,
311-
sortby=sortby,
312-
crs=dst_crs,
313-
lowercase=lowercase,
314325
):
315-
click.echo(json.dumps(feat, **dump_kwds))
316-
317-
318-
@cli.command()
319-
@verbose_opt
320-
@quiet_opt
321-
def clear_cache(verbose, quiet):
322-
bcdata.clear_cache()
326+
featurecollection = WFS.request_features(
327+
url=url,
328+
as_gdf=False,
329+
lowercase=lowercase,
330+
crs=dst_crs,
331+
promote_to_multi=promote_to_multi,
332+
)
333+
for feat in featurecollection["features"]:
334+
click.echo(json.dumps(feat, **dump_kwds))
323335

324336

325337
@cli.command()

0 commit comments

Comments
 (0)