-
-
Save oscarfonts/f150b9a7101453e3647af3b01c0face6 to your computer and use it in GitHub Desktop.
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
"""Calculate building floors from the Spanish Cadastre GIS files (SHP). | |
Once logged, data can be downloaded from https://www.sedecatastro.gob.es/ | |
and the data specification is at | |
http://www.catastro.minhap.gob.es/ayuda/manual_descriptivo_shapefile.pdf | |
This python script is to be used in the QGIS 2.x field calculator. | |
The "CONSTRU" field stores volumetric data and other attributes in the form | |
of a structured text code. Floor numbers are in roman numerals. | |
As for example, "-II+IV+TZA" is a building part with two underground floors, | |
four floors above ground and a roof terrace. This script analyzes the | |
"CONSTRU" code to get an integer for the total floors. | |
This code is based on the Mark Pilgrim's script | |
Convert to and from Roman numerals (http://diveintopython.org/) | |
Copyright (C) 2016 Benito M. Zaragozí | |
Authors: Benito M. Zaragozí (https://github.com/benizar) | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>.""" | |
from qgis.core import * | |
from qgis.gui import * | |
import re | |
#Define digit mapping | |
romanNumeralMap = (('M', 1000), | |
('CM', 900), | |
('D', 500), | |
('CD', 400), | |
('C', 100), | |
('XC', 90), | |
('L', 50), | |
('XL', 40), | |
('X', 10), | |
('IX', 9), | |
('V', 5), | |
('IV', 4), | |
('I', 1)) | |
#Cadastrial attributes with no height | |
building_attributes = re.compile( '(DARSENA|PTLAN|HORREO|TERRENY|CAMPING|GOLF|ZPAV|ZBE|PRESA|CONS|RUINA|ZD|VOL|FUT|YJD|JD|TRF|ESC|DEP|PRG|SUELO|SILO|ETQ|TEN|PI|ALT|SS|EPT|CO|MAR|PJE|SOP|POR|TZA|P|T|B)') | |
@qgsfunction(args="auto", group='Custom') | |
def get_cadastre_building_floors(s, feature, parent): | |
result=0 | |
#Remove all negative floors between -/+ characters or - alone | |
s=re.sub('-.+?\+', '', s) | |
s=s.split('-', 1)[0] | |
#Remove building attributes | |
s=building_attributes.sub( '', s) | |
#Split by + signs | |
s=s.split('+', 1) | |
#Loop on all strings. If it's a roman numeral, sum it | |
for i in s: | |
if isRoman(i): | |
result=result+fromRoman(i) | |
return result | |
#Define pattern to detect valid Roman numerals | |
romanNumeralPattern = re.compile(""" | |
^ # beginning of string | |
M{0,4} # thousands - 0 to 4 M's | |
(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's), | |
# or 500-800 (D, followed by 0 to 3 C's) | |
(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's), | |
# or 50-80 (L, followed by 0 to 3 X's) | |
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's), | |
# or 5-8 (V, followed by 0 to 3 I's) | |
$ # end of string | |
""" ,re.VERBOSE) | |
#Check if its a roman numeral | |
def isRoman(s): | |
result=1 | |
if not s: | |
result=0 | |
if not romanNumeralPattern.search(s): | |
result=0 | |
return result | |
#Convert Roman numeral to integer | |
def fromRoman(s): | |
result = 0 | |
index = 0 | |
for numeral, integer in romanNumeralMap: | |
while s[index:index+len(numeral)] == numeral: | |
result += integer | |
index += len(numeral) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment