En este caso tengo dos archivos iniciales. Uno con la posición de los colegio y otro con la de los jardines. Voy a juntar todo esta info en un solo GeoJSON: educacion.GeoJSON
In [1]:
import json
from bs4 import BeautifulSoup
import numpy as np
In [2]:
def read4json(file_path):
"Read configuration data form json file and return a list"
with open(file_path, 'r') as data_file:
data = json.load(data_file)
return data
def save2json(file_path, data):
"Save configuration data to a json file"
with open(file_path, 'w') as data_file:
json.dump(data, data_file, indent=4)
In [3]:
# Abro el archivo dejardines y lo inicializo con BeautifulSoup
data_path = '../datos/crudos/jardines.kml'
data = open(data_path).read()
bs = BeautifulSoup(data, "lxml")
In [4]:
# Extraigo los nombres y las coordinadas
names = [name.text for name in bs.find_all('name')][2:]
coordinates = [coord.text for coord in bs.find_all('coordinates')]
In [5]:
len(names), len(coordinates)
Out[5]:
In [6]:
# las coordinadas tienen el valor en z y no lo necesito
coordinates[0]
Out[6]:
In [7]:
features = []
for name, coord in zip(names, coordinates):
# elimino el parametro de z
lat, lng, z = coord.split(',')
new_element = {
"geometry": {
"coordinates": [float(lat), float(lng)],
"type": "Point"
},
"type": "Feature",
"properties": {
"name": "Centro Infantil " + name.title(),
"type": "educacion"
}
}
features.append(new_element)
In [8]:
# Ahora agrego los colegios
data_path = '../datos/crudos/escuelas.kml'
data = open(data_path).read()
bs = BeautifulSoup(data, "lxml")
In [9]:
# Extraigo los nombres y las coordinadas
names = [name.text for name in bs.find_all('name')][2:]
coordinates = [coord.text for coord in bs.find_all('coordinates')]
In [10]:
len(names), len(coordinates)
Out[10]:
In [11]:
for name, coord in zip(names, coordinates):
# elimino el parametro de z
lat, lng, z = coord.split(',')
new_element = {
"geometry": {
"coordinates": [float(lat), float(lng)],
"type": "Point"
},
"type": "Feature",
"properties": {
"name": "Escuela " + name.title(),
"type": "educacion"
}
}
features.append(new_element)
In [12]:
features[-1]
Out[12]:
In [13]:
geojson = {
"type": "FeatureCollection",
"features": features
}
save2json('../datos/educacion.geojson', geojson)
In [ ]: