Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from anisotropi4/main
Browse files Browse the repository at this point in the history
Create GeoPKG files
  • Loading branch information
poggs authored Jul 4, 2022
2 parents 6749395 + 0eb3b02 commit 345db65
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# GeoPKG files
*.gpkg

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
geopandas >= 0.11.0
49 changes: 49 additions & 0 deletions shp2gpkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
"""Grovel directories containing ESRI ShapeFile to create GeoPackage layers"""

from os import walk
import re
import argparse
import pandas as pd
import geopandas as gp


pd.set_option('display.max_columns', None)

PARSER = argparse.ArgumentParser(description='Create ESRI shp data as layers in a GeoPackage file')
PARSER.add_argument('--output', type=str, default='geo-model.gpkg', nargs='?',
help='output filename')
PARSER.add_argument('--search', type=str, default='.', nargs='?', help='search path')
PARSER.add_argument('--crs', type=str, default='EPSG:32630',
nargs='?', help='coordinate reference system')

ARGS, REST = PARSER.parse_known_intermixed_args()
REST = (REST + [None] * 3)[:3]
OUTPATH, FILEPATH, CRS = [(i or j) for i, j in zip(REST, vars(ARGS).values())]

def list_files(filepath, match):
"""find all filenames containing match in directories under filepath """
files = ()
for (d, _, filenames) in walk(filepath):
if 'geopandas/datasets' in d:
continue
files = files + tuple(f'{d}/{f}' for f in filenames if match in f)
return files

PATTERNS = [re.compile(i, re.IGNORECASE) for i in ['shapefile', 'shape$', 'file$']]

def get_layername(filepath):
"""return layer name from shape-filepath"""
r = filepath.split('/')[-1]
r = r.replace('.shp', '')
for p in PATTERNS:
r = p.split(r)[0]
return r

FILES = [f for f in list_files(FILEPATH, 'shp') if f[-4:] == '.shp']


for f in FILES:
gf = gp.read_file(f)
layername = get_layername(f)
gf.to_crs(CRS).to_file(OUTPATH, driver='GPKG', layer=layername)
21 changes: 21 additions & 0 deletions shp2gpkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/bash

if [ ! -d venv ]; then
echo Set up python3 virtual environment
python -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
else
source venv/bin/activate
fi

if [ ! -s network-model.gpkg ]; then
echo Create network model GeoPKG
python shp2gpkg.py network-model.gpkg network-model/
fi
if [ ! -s organisational-boundaries.gpkg ]; then
echo Create organisational boundaries GeoPKG
python shp2gpkg.py organisational-boundaries.gpkg organisational-boundaries/
fi

0 comments on commit 345db65

Please sign in to comment.