-
Notifications
You must be signed in to change notification settings - Fork 0
/
00importCadastre.py
123 lines (94 loc) · 3.06 KB
/
00importCadastre.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
'''
use ogr to import cadastre shapefiles to spatialite
'''
import routes
import glob
import zipfile
import os
import subprocess as sub
def unzip(f):
'''
descomprime el archivo f.zip en la misma carpeta
'''
zfile = zipfile.ZipFile(f)
print '/'.join(f.split('/')[:-1])
zfile.extractall( routes.r_datain+'files')
return
def createFolder(folder):
dir_exists = os.path.isdir(folder)
if dir_exists is False:
os.makedirs(folder)
print '>>> created folder :', folder
return
def getGeoType(File):
#ogrR = "ogrinfo -ro PG:'host="+host+" user="+user+" dbname="+dbname+"'"
ogrR =["ogrinfo" ,File]
p = sub.Popen(ogrR, stdout=sub.PIPE,stderr = sub.PIPE)
output, errors = p.communicate()
print output
out = output.split('(')[1].strip(') \n')#[:-2]
if out == 'Polygon':
result = 'MULTIPOLYGON'
elif out == 'Line String':
result = 'MULTILINESTRING'
elif out =='Point':
result = 'MULTIPOINT'
elif out == 'Multi Polygon':
result = 'MULTIPOLYGON'
return result
def insert2sqlite(fileName):
'''
ogr2ogr -f SQLite -dsco SPATIALITE=YES db_name.sqlite /path/to/gis_data -nlt PROMOTE_TO_MULTI
ogr2ogr -update -append -f SQLite mydb.sqlite -nln "newtable" -a_srs "EPSG:4326" shapefile.shp
:param fileName:
:return:
'''
print ('>>> importing : ', fileName)
#check if exists
Type = getGeoType(fileName)
print Type
#Type = 'Point'
ogrR = "ogr2ogr -overwrite -f SQLITE " # -overwrite
ogrR += '-dsco SPATIALITE=YES '
ogrR += (routes.r_proj+'cartobase/'+fileName.replace('.SHP', '.sqlite').lower().split('/')[-1]).replace(' ', '\ ')
ogrR += " -t_srs 'epsg:25831'"
ogrR += " -s_srs 'epsg:25831'"
ogrR += " -dim 2"
ogrR += " -lco LAUNDER=YES"
ogrR += " -lco GEOMETRY_NAME=geom"
ogrR += ' -nlt ' + Type +' '
ogrR += " -nln " + 'elements '
ogrR += fileName.replace(' ', '\ ')
ogrR += ' -progress'
print ogrR
os.system(ogrR)
return
############################################################################
############################################################################
############################################################################
############################################################################
def import():
files ={
'MASA.zip' :'',
'PARCELA.zip' :'',
'ELEMLIN.zip' :'',
'CONSTRU.zip' :''
}
createFolder(routes.r_datain+'files')
#uncompress
folder = ''
for file in glob.glob(routes.r_datain+"*.zip"):
unzip(file)
for file in glob.glob(routes.r_datain+"files/*.zip"):
if file.split('/')[-1] in files.keys():
unzip(file)
#import
shapefiles = []
for s in glob.glob(routes.r_datain+"files/*.SHP"):
shapefiles.append(s.split('/')[-1])
for file in files.keys():
if file.split('.')[0]+'.SHP' in shapefiles:
insert2sqlite(routes.r_datain + "files/" + file.split('.')[0] + '.SHP')
return
if __name__ == "__main__":
import()