Skip to content

Commit

Permalink
add export (to SQLite or geopackage) function to the geoslurper client
Browse files Browse the repository at this point in the history
  • Loading branch information
Roelof Rietbroek committed Nov 23, 2023
1 parent 7f1934f commit 730c062
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
8 changes: 7 additions & 1 deletion clitools/geoslurper.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def main(argv):
sys.exit(0)


if not (args.pull or args.register or args.purge_cache or args.purge_data or args.purge_entry):
if not (args.pull or args.register or args.purge_cache or args.purge_data or args.purge_entry or args.export):
sys.exit(0)

if args.pull:
Expand Down Expand Up @@ -226,6 +226,10 @@ def main(argv):
ds.register(**regopts)
except KeyboardInterrupt:
ds.halt()

if args.export:
ds.export(args.export)

#We need to explicitly delete the dataset instance or else the database QueuePool gets exhausted
del ds

Expand Down Expand Up @@ -329,6 +333,8 @@ def addCommandLineArgs():

parser.add_argument("--register", metavar="JSON",action=JsonParseAction, nargs="?",const=False, default=False,
help="Register data in the database (possibly pass on options as a JSON dict)")
parser.add_argument("--export", metavar="OUTPUTFILE",type=str, nargs="?",const="auto", default=False,
help="Export the selected tables in a SQLITE or geopackage file. The type of output is determined from the OUTPUTFILE extension (.sql or .gpkg). When no OUTPUTFILE is provided an SQLITE or gpkg file is dumped in the current directory (depending on whether thee table has a geometry columns.")

# parser.add_argument("--update", metavar="JSON", action=JsonParseAction, nargs="?",const=False,default=False,
# help="Implies both --pull and --register, but applies only to the updated data (accepts JSON options)")
Expand Down
30 changes: 21 additions & 9 deletions geoslurp/dataset/dataSetBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from sqlalchemy import and_
from geoslurp.db.settings import getCreateDir
from geoslurp.db import tableMapFactory

from geoslurp.db.exporter import exportQuery

def rmfilterdir(ddir,filter='*'):
"""Remove directories and files based on a certain regex filter"""
Expand Down Expand Up @@ -337,14 +337,26 @@ def migrate(self,version):
raise RuntimeError("No migration implemented")
if version > self.version:
raise RuntimeError("Registered database has a higher version number than supported")

def export(self,outputfile):
"""Export the table to a different format"""
qry=f"SELECT * FROM {self.scheme}.{self.name};"
qryres=self.db.dbeng.execute(qry)
layer=f"{self.name}"
if outputfile == "auto":
#create a name based on the current scheme and table name
if "geom" in qryres.keys():
outputfile=f"{self.scheme}_{self.name}.gpkg"
else:
outputfile=f"{self.scheme}_{self.name}.sql"
if outputfile.endswith(".gpkg"):
driver="GPKG"
elif outputfile.endswith(".sql"):
driver="SQLITE"
else:
raise RuntimeError("in Export: Unknown outputfile selected")


# def loadTable(self):
# """Reflects a dataset table from the database"""
# if self.table:
# slurplogger().warning("Reflecting and overwriting existing database table with data from server")
slurplogger().info(f"Exporting table to {outputfile}")
exportQuery(qryres,outputfile,layer=layer,driver=driver)


# if custumcolumns in self._dbinvent.data:
# #override columns with customn types
# cols=
2 changes: 1 addition & 1 deletion geoslurp/db/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import geopandas as gpd
import pandas as pd
from geoslurp.tools.shapelytools import shpextract
from geoslurp.db.settings import MirrorMap
# from geoslurp.db.settings import MirrorMap
import re
import tarfile
from sqlalchemy import create_engine
Expand Down

0 comments on commit 730c062

Please sign in to comment.