/
cadastralDownloaderManager.py
210 lines (179 loc) · 6.46 KB
/
cadastralDownloaderManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# encoding: utf-8
import gvsig
from org.gvsig.andami import Utilities
from java.lang import Thread
import threading
import zipfile
import os
import pprint, os, urllib2, base64
from org.gvsig.fmap.dal import DALLocator
from java.lang import Object
from org.gvsig.app import ApplicationLocator
class OMunicipality(Object):
def __init__(self,f):
self.codprov = f[0]
self.prov = f[1]
self.codmun = f[2]
self.mun = f[3]
self.id = f[4]
def getMun(self):
return self.mun
def getProv(self):
return self.prov
def getCodProv(self):
return self.codprov
def getCodMun(self):
return self.codmun
def getId(self):
return self.id
def toString(self):
return self.mun
def download(url):
request = urllib2.Request(url)
#base64string = base64.b64encode('%s:%s' % (username, password))
#request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
return result
class CadastralDownloaderManager():
def __init__(self, view, app=None):
self.app = app
self.view = view
self.st = None
self.downloadList={} #name, url
self.downloaded={}
self.working=False
self.stopThread=False
print "***done manager"
def getDownloaded(self):
return self.downloaded
def addDownload(self, value, url, oml):
self.downloadList[value]=[url, oml]
if self.working==False:
self.working=True
self.setWorking()
def getDownloadPath(self):
return Utilities.TEMPDIRECTORYPATH
def getNewThread(self):
#self.st= SentinelThread()
pass
def download(self, value, url, oml):
print "Downloading... ", value
self.setWorking(True)
f = download(url)
total_size = int(f.headers["Content-Length"])
MB_size = round(total_size/1048576, 2)
block_size = 1024 * 1024
outputPathFull=os.path.join(self.getDownloadPath(),value+".zip")
with open(outputPathFull, "wb") as file:
while True:
block = f.read(block_size)
dSize = round(int(os.stat(outputPathFull).st_size)/1048576, 2)
statusText = " (" + str(dSize) + "/" + str(MB_size) + " MB) " + " Downloading.."+ value # + url + "Downloading"
if self.app != None:
self.app.txtDownloadStatus.setText(statusText)
if self.stopThread==True:
self.setStopThread()
break
else:
print statusText
if not block:
break
file.write(block)
if self.stopThread==True:
os.remove(outputPathFull)
self.setDownloadFinished(value, url, oml, outputPathFull)
def setStopThread(self, status=False):
self.stopThread = status
if self.app!=None:
self.app.txtDownloadStatus.setText("")
def setDownloadFinished(self, value, url, oml, outputPathFull):
self.downloadList.pop(value)
self.downloaded[value]=url
self.setWorking(False)
self.loadFileIntoH2(oml, outputPathFull)
def loadFileIntoH2(self, oml, outputPathFull):
path=gvsig.getTempFile(oml.getCodMun(),'')#os.path.join(self.getDownloadPath(),oml.get)
print "PATH FOR GML:", path
os.mkdir(path)
zip_ref = zipfile.ZipFile(outputPathFull, 'r')
zip_ref.extractall(path)
zip_ref.close()
gmlFile = 'A.ES.SDGC.CP.{0}.cadastralparcel.gml'.format(oml.getCodMun())
gmlFilePath = os.path.join(path, gmlFile)
if not os.path.exists(gmlFilePath):
print "fail"
return
manager = DALLocator.getDataManager()
storeParameters = manager.createStoreParameters("GMLDataStoreProvider")
#storeParameters.setDynValue("connectionString",None)
storeParameters.setDynValue("CRS",self.view.getProjection())
#storeParameters.setDynValue("defaultGeometryField",None)
storeParameters.setDynValue("file",gmlFilePath)
#storeParameters.setDynValue("gfsSchema",None)
#storeParameters.setDynValue("ignoreSpatialFilter",True)
storeParameters.setDynValue("layerName",oml.getCodMun())
#storeParameters.setDynValue("ProviderName",None)
#storeParameters.setDynValue("xsdSchema",None)
storeGML = manager.openStore("GMLDataStoreProvider",storeParameters)
#application = ApplicationLocator.getManager()
#mapcontextmanager = application.getMapContextManager()
#layer = mapcontextmanager.createLayer(oml.getId(),store)
#self.view.addLayer(layer)
# Open Store
storeParameters = manager.createStoreParameters("H2Spatial")
database_file=gvsig.getResource(__file__, "data", "municipalities.mv.db")
storeParameters.setDynValue("database_file",database_file)
storeParameters.setDynValue("Table","Muni")
storeH2 = manager.openStore("H2Spatial",storeParameters)
storeH2.edit()
fsetGML = storeGML.getFeatureSet()
for f in fsetGML:
value = {
'CODPROV': oml.getCodProv(),
'PROV': oml.getProv(),
'CODMUN': oml.getCodMun(),
'MUN': oml.getMun(),
'ID': oml.getId(),
'gml_id': f.get('gml_id'),
'GEOMETRY': f.getDefaultGeometry()
}
storeH2.append(value)
storeH2.commit()
storeGML.dispose()
storeH2.dispose()
def setWorking(self, state=None):
self.working=True
if state==False or state==None:
if len(self.downloadList.keys())>0:
self.working = True
self.downloading()
else:
self.working = False
elif state==True:
self.working = True
def downloading(self):
value = self.downloadList.keys()[0]
url = self.downloadList[value][0]
oml = self.downloadList[value][1]
#self.downloadList.pop(value)
threading.Thread(target=self.download, name="DownloadCatastro", args=(value, url, oml)).start()
def getCadastralLink(self, oml):
#ObjectListMunicipality
#"CODPROV","PROV","CODMUN","MUN","ID"
v1 = oml.getCodProv()
v2 = oml.getCodMun()
v3 = oml.getMun()
link = 'http://www.catastro.minhap.es/INSPIRE/CadastralParcels/{0}/{1}-{2}/A.ES.SDGC.CP.{1}.zip'.format(
v1,
v2,
v3)
return link
def main(*args):
s = CadastralDownloaderManager(gvsig.currentView())
print s.getDownloadPath()
#href="http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=wfs&request=getfeature&Typenames=cp.cadastralparcel&SRSname=EPSG::25830&bbox=233673,4015968,233761,4016008"
#s.addDownload("S1", href)
href="http://www.catastro.minhap.es/INSPIRE/CadastralParcels/56/56101-MELILLA/A.ES.SDGC.CP.56101.zip"
#href=prepareCadastralLink()
oml=OMunicipality(['02','Albacete','02025','CAUDETE','02025-CAUDETE'])
s.addDownload("S2", href, oml)