Tengo un diccionario con puntos. Los puntos proceden de una trama (véase la imagen de abajo). Los puntos no están ordenados. El diccionario tiene, por ejemplo, este aspecto:
pointDict = {0: (345645.1276541934, 1267223.104499615), 1: (345626.87681620114, 1267223.2540966477), 2: (345645.2772512261, 1267268.581997563), 3: (345617.751397205, 1267223.4036936804), 4: (345654.1034761568, 1267259.306981534), 5: (345636.15183223, 1267231.781127513), 6: (345636.30142926273, 1267268.2828034975), 7: (345626.87681620114, 1267259.306981534), 8: (345617.90099423775, 1267259.306981534), 9: (345608.7755752416, 1267259.6061755994), 10: (345599.7997532782, 1267250.1815625378), 11: (345590.6743342821, 1267250.4807566034)}
Quiero crear una multilínea. La distancia máxima de punto a punto es de 14 m. Si los puntos están más separados, pasan a una nueva línea.
Hasta ahora tengo el siguiente código. Funciona, pero el problema es que los puntos están conectados en el orden equivocado, como se puede ver en la imagen de abajo.
import ogr, gdal, os
from math import sqrt
pointDict = {0: (345645.1276541934, 1267223.104499615), 1: (345626.87681620114, 1267223.2540966477), 2: (345645.2772512261, 1267268.581997563), 3: (345617.751397205, 1267223.4036936804), 4: (345654.1034761568, 1267259.306981534), 5: (345636.15183223, 1267231.781127513), 6: (345636.30142926273, 1267268.2828034975), 7: (345626.87681620114, 1267259.306981534), 8: (345617.90099423775, 1267259.306981534), 9: (345608.7755752416, 1267259.6061755994), 10: (345599.7997532782, 1267250.1815625378), 11: (345590.6743342821, 1267250.4807566034)}
multiline = ogr.Geometry(ogr.wkbMultiLineString)
i = 0
lineDict = {}
for item in pointDict:
stop = False
x = pointDict[item][0]
y = pointDict[item][1]
if item != 0:
xPrevious = pointDict[item-1][0]
yPrevious = pointDict[item-1][1]
distance = sqrt((y-yPrevious)**2+(x-xPrevious)**2)
for line in multiline:
if line.GetPointCount() > 0:
j = 0
for j in range(0, line.GetPointCount()):
point = line.GetPoint(j)
xExisting = point[0]
yExisting = point[1]
distance = sqrt((y-yExisting)**2+(x-xExisting)**2)
j += 1
if distance < 14:
line.AddPoint(x,y)
stop = True
if not stop:
lineDict[i] = ogr.Geometry(ogr.wkbLineString)
lineDict[i].AddPoint(x,y)
multiline.AddGeometry(lineDict[i])
i += 1
for line in multiline:
print line
outSHPfn = 'test1.shp'
shpDriver = ogr.GetDriverByName("ESRI Shapefile")
if os.path.exists(outSHPfn):
shpDriver.DeleteDataSource(outSHPfn)
outDataSource = shpDriver.CreateDataSource(outSHPfn)
outLayer = outDataSource.CreateLayer(outSHPfn, geom_type=ogr.wkbMultiLineString )
featureDefn = outLayer.GetLayerDefn()
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(multiline)
outLayer.CreateFeature(outFeature)
Cualquier idea es bienvenida. Quiero hacer esto en Python idealmente con GDAL/OGR (no ArcPy).
0 votos
Su script tiene errores
y = pointDict[item][2]
->y = pointDict[item][1]
yyPrevious = pointDict[item-1][3] -> yPrevious = pointDict[item-1][1]
o no funciona.0 votos
@gene: Corregidos los números mezclados.
0 votos
El código final
raster2dictionary2lines
se puede encontrar aquí . Lo solucioné con la respuesta publicada por gene.