Created
June 20, 2017 10:27
-
-
Save trolleway/5f50bf834f8852d0824e427a13aa6277 to your computer and use it in GitHub Desktop.
shapes2geopackage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
''' | |
from __future__ import unicode_literals | |
import config | |
import os | |
import shutil | |
import math | |
from xml.etree.ElementTree import ElementTree | |
import argparse | |
import datetime | |
def argparser_prepare(): | |
class PrettyFormatter(argparse.ArgumentDefaultsHelpFormatter, | |
argparse.RawDescriptionHelpFormatter): | |
max_help_position = 35 | |
parser = argparse.ArgumentParser(description='Конвертирует все шейп-файлы из папки в один Geopackage', | |
formatter_class=PrettyFormatter) | |
parser.add_argument('--path', type=str,required=False, default='newlayers/data',help='path to folder') | |
parser.add_argument('--filename', type=str,required=False, default='newlayers/osm.gpkg', help='result filename') | |
parser.epilog = \ | |
'''Samples: | |
%(prog)s --path export --filename spec.gpkg | |
''' \ | |
% {'prog': parser.prog} | |
return parser | |
parser = argparser_prepare() | |
args = parser.parse_args() | |
path = args.path | |
filename = args.filename | |
import shlex | |
#from __future__ import print_function # Only Python 2.x | |
import subprocess | |
#Рассчёт времени на работу скрипта | |
import time | |
start_time = time.time() | |
def execute(cmd): | |
#execute system command and return every string of it output just as it comes | |
popen = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, universal_newlines=True) | |
for stdout_line in iter(popen.stdout.readline, b""): | |
yield stdout_line | |
popen.stdout.close() | |
return_code = popen.wait() | |
if return_code: | |
raise subprocess.CalledProcessError(return_code, cmd) | |
shapefiles=[] | |
for file in os.listdir(path): | |
if file.endswith(".shp"): | |
shapefiles.append(os.path.join(path, file)) | |
print shapefiles | |
for shapefile in shapefiles: | |
cmd='ogr2ogr -progress -f "GPKG" {filename} {shapefile} -append -update'.format(filename=filename, shapefile=shapefile) | |
print cmd | |
for response in execute(cmd): | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment