forked from apinhal/predios2gpkg_DGT-cadastro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredios2gpkg.py
executable file
·66 lines (57 loc) · 2.06 KB
/
predios2gpkg.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
#!/usr/bin/env python
# convert DGT-cadastro JSON to GeoPackage
# run: predios2gpkg.py <url>
"""
+info:
https://www.dgterritorio.gov.pt/cadastro
https://snic.dgterritorio.gov.pt/visualizadorCadastro
run:
predios2gpkg.py "https://snic.dgterritorio.gov.pt/geoportal/dgt_cadastro/api/v1/cadastro/cgpr/predios?dico=1214&dicofre=121411&seccao=C"
"""
import sys, os
import requests, json
try:
from osgeo import ogr,osr
except:
import ogr, osr
# read JSON
#response = requests.get("https://snic.dgterritorio.gov.pt/geoportal/dgt_cadastro/api/v1/cadastro/cgpr/predios?dico=1214&dicofre=121411&seccao=C")
response = requests.get(sys.argv[1])
predios = json.loads(response.text)
# gpkg output
#gpkgFile = "predios.gpkg"
gpkgFile = predios[0]["dicofre"] + "-" + predios[0]["seccao"] + ".gpkg"
driver = ogr.GetDriverByName("GPKG")
if os.path.exists(gpkgFile):
driver.DeleteDataSource(gpkgFile)
dataSource = driver.CreateDataSource(gpkgFile)
## set EPSG:3763 CRS
srs = osr.SpatialReference()
srs.ImportFromEPSG(3763)
## create polygon layer
layer = dataSource.CreateLayer(gpkgFile, srs, ogr.wkbPolygon)
## create layer attributes fields
layer.CreateField(ogr.FieldDefn("dicofre", ogr.OFTString))
layer.CreateField(ogr.FieldDefn("seccao", ogr.OFTString))
layer.CreateField(ogr.FieldDefn("predio", ogr.OFTString))
layer.CreateField(ogr.FieldDefn("area_m2", ogr.OFTInteger))
for predio in predios:
# JSON fields
#dicofre = predio["dicofre"]
#seccao = predio["seccao"]
#nprd = predio["nprd"]
#area = predio["area_m2"]
wkt = predio["wkt"]
# create feature
feature = ogr.Feature(layer.GetLayerDefn())
# set feature attributes from json
feature.SetField("dicofre", predio["dicofre"])
feature.SetField("seccao", predio["seccao"])
feature.SetField("predio", predio["nprd"])
feature.SetField("area_m2", predio["area_m2"])
# set feature geometry (polygon) from Well Known Txt
feature.SetGeometry(ogr.CreateGeometryFromWkt(wkt))
layer.CreateFeature(feature)
feature.Destroy()
# save and close data source
dataSource.Destroy()