Key:ref:EUS:getxo

From OpenStreetMap Wiki
Jump to navigation Jump to search

This is an identifier prefix that serves as base for my imports of house numbers from Getxo town open data.

I use it only to document how I imported some of the house numbers from the Getxo town:


Origin

The data originates from https://www.getxo.eus/es/gobierno-abierto/opndata/cartografia-getxo .

Process

Download source

I downloaded from the section "TERRITORIO Y ELEMENTOS GEOPOSICI" the "Portales" data in KML format. The GeoJSON file seems to have wrong coordinates, they all point into the see.

Transformation

I transformed the file with the following Python script :

import xml.etree.ElementTree as ET
import xml.dom.minidom
from collections import defaultdict

def parse_kml(file_path):
	tree = ET.parse(file_path)
	root = tree.getroot()
	return tree, root

def extract_points(root):
	namespace = {'kml': 'http://www.opengis.net/kml/2.2'}
	placemarks = root.findall('.//kml:Placemark', namespace)
	points = defaultdict(list)
	
	for placemark in placemarks:
		coordinates = placemark.find('.//kml:coordinates', namespace).text.strip()
		extended_data = placemark.find('.//kml:ExtendedData', namespace)
		props = {}
		if extended_data is not None:
			schema_data = extended_data.find('.//kml:SchemaData', namespace)
			if schema_data is not None:
				for simple_data in schema_data.findall('.//kml:SimpleData', namespace):
					key = simple_data.attrib['name']
					value = simple_data.text
					if value is not None:
						props[key] = value
		points[coordinates].append(props)
	
	return points

def merge_properties(points):
	merged_points = {}
	for coord, props_list in points.items():
		merged_props = defaultdict(set)
		for props in props_list:
			for key, value in props.items():
				if value is not None:
					merged_props[key].add(value)
		
		# Concatenate values for each property, handling None values
		merged_props = {k: ', '.join(v) for k, v in merged_props.items() if v}
		merged_points[coord] = merged_props
	
	return merged_points

def format_street_name(name):
	return ' '.join(word.capitalize() for word in name.lower().split())

def map_to_osm_tags(props):
	if False:
		tipo_via_mapping = {
			'CR': 'Carretera',
			'CL': 'Calle',
			'AV': 'Avenida',
			'PL': 'Plaza',
			'PS': 'Paseo',
			'CT': 'Camino',
			'RD': 'Ronda',
			'BJ': 'Bajada',
			'TR': 'Travesía',
			'UR': 'Urbanización',
			'PT': 'Puente',
			'GL': 'Glorieta',
			'BG': 'Bajada',
			'PQ': 'Parque',
			'AL': 'Alameda',
			'CA': 'Carrera',
			'CM': 'Camino',
			'CU': 'Cuesta',
			'RB': 'Rambla',
			'CH': 'Chaussee'#,
			#'CL': 'Callejón'
		}
	else:
		tipo_via_mapping = {
			'CR': 'errepidea',	# Carretera
			'CL': 'kalea',		# Calle
			'AV': 'avenida',	# Avenida
			'PL': 'plaza',		# Plaza
			'PS': 'pasealekua', # Paseo
			'CT': 'bidea',		# Camino
			'RD': 'erronda',	# Ronda
			'BJ': 'jaitsiera',	# Bajada
			'TR': 'zehaerra',	# Travesía
			'UR': 'hirigunea',	# Urbanización
			'PT': 'zubia',		# Puente
			'GL': 'biribilgune' # Glorieta
		}
	
	tags = {}
	if 'id' in props:
		tags['ref:EUS:getxo:id'] = props['id']
	if 'cod_via' in props:
		tags['ref:EUS:getxo:cod_via'] = props['cod_via']
	if 'tipo_via' in props:
		tags['ref:EUS:getxo:tipo_via'] = props['tipo_via']
	if 'via' in props:
		street_name = format_street_name(props['via'])
		if 'tipo_via' in props and props['tipo_via'] in tipo_via_mapping:
			street_name = f"{tipo_via_mapping[props['tipo_via']]} {street_name}"
		tags['addr:street'] = street_name		
	if 'portal' in props:
		tags['addr:housenumber'] = props['portal']
		if 'duplicado' in props:
			tags['addr:housenumber'] += props['duplicado']
	if 'distrito' in props:
		tags['ref:EUS:getxo:distrito'] = props['distrito']
	if 'seccion' in props:
		tags['ref:EUS:getxo:seccion'] = props['seccion']
	if 'vivienda_en_lonja' in props:
		tags['ref:EUS:getxo:vivienda_en_lonja'] = props['vivienda_en_lonja']
	if 'no_tiene_viviendas' in props:
		tags['ref:EUS:getxo:no_tiene_viviendas'] = props['no_tiene_viviendas']
	
	tags['source_ref'] = "https://www.getxo.eus/es/gobierno-abierto/opndata/cartografia-getxo"
	tags['addr:city'] = "Getxo"
	
	return tags

def create_kml(merged_points, output_path):
	kml = ET.Element('kml', xmlns='http://www.opengis.net/kml/2.2')
	document = ET.SubElement(kml, 'Document')
	
	for coord, props in merged_points.items():
		placemark = ET.SubElement(document, 'Placemark')
		
		# Create new ExtendedData with OSM tags only
		extended_data = ET.SubElement(placemark, 'ExtendedData')
		schema_data = ET.SubElement(extended_data, 'SchemaData', schemaUrl="#getxo_opendata_est_callejero")
		
		osm_tags = map_to_osm_tags(props)
		for key, value in osm_tags.items():
			simple_data = ET.SubElement(schema_data, 'SimpleData', name=key)
			simple_data.text = value
		
		point = ET.SubElement(placemark, 'Point')
		coordinates = ET.SubElement(point, 'coordinates')
		coordinates.text = coord
	
	if False:
		# No pretty print
		tree = ET.ElementTree(kml)
		tree.write(output_path, encoding='utf-8', xml_declaration=True)
	else:
		# With pretty print
		# Convert the ElementTree to a string
		rough_string = ET.tostring(kml, encoding='utf-8')
		
		# Parse the string into a minidom object
		reparsed = xml.dom.minidom.parseString(rough_string)
		
		# Pretty print the minidom object to the output file
		with open(output_path, 'w', encoding='utf-8') as f:
			f.write(reparsed.toprettyxml(indent="  "))	

def main(input_path, output_path):
	tree, root = parse_kml(input_path)
	points = extract_points(root)
	merged_points = merge_properties(points)
	create_kml(merged_points, output_path)



input_kml = 'TerritorioyElementosGeoposicionadosPortales140720241145878.kml' # <== change input file path here
output_kml = 'gexto-portales-converted.kml' # <== change output file path here
main(input_kml, output_kml)

You need python 3, pip and geopy library to be installed with pip:

   pip install geopy

Import

I opened the result file in the Openstreetmap ID editor .

Then I created by hand points nearby the displayed points and copy pasted all properties.