Created
November 13, 2015 14:41
-
-
Save trolleway/6589bbe21cec82dd1e73 to your computer and use it in GitHub Desktop.
wms downloader with resume
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# anchor extraction from html document | |
from bs4 import BeautifulSoup | |
import urllib2 | |
import urlparse | |
import re | |
import csv | |
import time | |
import sys | |
from owslib.wms import WebMapService | |
import os.path | |
def progress(count, total, suffix=''): | |
''' | |
Display progress bar | |
sources: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3 | |
''' | |
bar_len = 60 | |
filled_len = int(round(bar_len * count / float(total))) | |
percents = round(100.0 * count / float(total), 1) | |
bar = '=' * filled_len + '-' * (bar_len - filled_len) | |
sys.stdout.write('[%s] %s%s %s\r' % (bar, percents, '%', suffix)) | |
sys.stdout.flush() | |
def get_args(): | |
import argparse | |
p = argparse.ArgumentParser(description='Download raster from WMS server with resume downloading ') | |
p.add_argument('url', help='WMS URL') | |
p.add_argument('id', help='layer ID') | |
return p.parse_args() | |
if __name__ == '__main__': | |
args = get_args() | |
#open getcapabilities | |
url='http://example.com/wms?' | |
layerId='Primorye_HighDef_Partizansk' | |
style='raster' | |
wms = WebMapService(url, version='1.1.1') | |
bbox=wms[layerId].boundingBox | |
x1=bbox[0] #точность | |
y1=bbox[1] | |
x2=bbox[2] | |
y2=bbox[3] | |
#calc extent | |
#create folder | |
#start cycle for 1km | |
height=500 | |
width=500 | |
#foldername=datetime.now().isoformat() | |
#calc count | |
leftx=x1 | |
fragmentTotalCount=0 | |
while(leftx < x2): | |
#print 'leftx'+str(leftx) | |
lowy=y1 | |
while(lowy < y2): | |
#print 'lowy'+str(lowy) | |
fragmentTotalCount=fragmentTotalCount+1 | |
lowy=lowy+height | |
leftx=leftx+width | |
fragmentCounter=0 | |
leftx=x1 | |
while(leftx < x2): | |
#print 'leftx'+str(leftx) | |
lowy=y1 | |
while(lowy < y2): | |
#print 'lowy'+str(lowy) | |
fragmentCounter = fragmentCounter+1 | |
fragmentFilename=str(int(leftx))+'x'+str(int(lowy)) | |
fragmentFilename='fragment_'+fragmentFilename+'.tiff' | |
fragmentBbox=(leftx,lowy,leftx+width,lowy+height) | |
#print fragmentFilename +' '+ str(fragmentCounter)+'/'+str(fragmentTotalCount) | |
progress(fragmentCounter, fragmentTotalCount,str(fragmentCounter)+'/'+str(fragmentTotalCount)) | |
if (os.path.isfile(fragmentFilename)): | |
1+1#print 'skip' | |
#time.sleep(1) | |
else: | |
img = wms.getmap( layers=[layerId], | |
styles=[style], | |
srs='EPSG:3857', | |
bbox=fragmentBbox, | |
size=(500, 500), | |
format='image/geotiff', | |
transparent=True | |
) | |
out = open('temp.tiff', 'wb') | |
out.write(img.read()) | |
out.close() | |
os.rename('temp.tiff',fragmentFilename) | |
lowy=lowy+height | |
leftx=leftx+width | |
#check is raster exists | |
#calc tile extent | |
quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment