¿Alguna sugerencia para crear espacios de búfer en KML LineString usando Python? Probé con pocas bibliotecas …
geojson
shapely
kml2geojson
Aquí, KML convertido a GeoJSON y espacio de búfer agregado en la cadena lineal y la salida debe ser un polígono
import kml2geojson
import json
from shapely.geometry import shape
from geojson import Point, Feature, FeatureCollection, dump
filename = "filekml"
direction = "left"
// numbers in meter
numbers = 5
kml2geojson.main.convert(filename + '.kml', '')
features = []
try:
with open(filename + '.geojson') as geojson_file:
data = json.load(geojson_file)
for feat in data['features']:
if direction == "left":
result = shape(feat['geometry']).buffer(numbers, single_sided=True)
if direction == "right":
result = shape(feat['geometry']).buffer(-numbers, single_sided=True)
if direction == "both":
result = shape(feat['geometry']).buffer(numbers)
features.append(Feature(geometry=result))
feature_collection = FeatureCollection(features)
with open(filename + '.geojson', 'w') as f:
dump(feature_collection, f)
f.close()
except Exception as e:
print(e)
A continuación se muestra el KML
<?xml version="1.0" encoding="utf-8" ?>
<kml>
<Document id="root_doc">
<Folder><name>Test</name>
<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<MultiGeometry><LineString><coordinates>-93.3367092468336,30.4822077397353 -93.3367001199999,30.482718171</coordinates></LineString></MultiGeometry>
</Placemark>
</Folder>
</Document></kml>
A continuación se muestra la salida (GeoJSON)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-93.3367,
30.482718
],
[
-93.336709,
30.482208
],
[
-93.336722,
30.482208
],
[
-93.336713,
30.482718
],
[
-93.3367,
30.482718
]
]
]
},
"properties": {}
}
]
}
¿Alguna sugerencia para crear espacios intermedios de línea o puntos?
1 respuesta
Si desea almacenar en una zona de influencia una línea, ya sea lateralmente a la izquierda o a la derecha, o en todas las direcciones por una distancia en metros, la biblioteca GeoPandas lo simplifica. Una vez que haya convertido el KML a GeoJSON, lea el archivo GeoJSON en un GeoDataFrame, vuelva a proyectar los datos en un sistema de coordenadas con metros como unidades, realice el búfer y luego vuelva a convertirlo a WGS84 (EPSG-4326).