Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/06/2009, 17:49
miquegordis
 
Fecha de Ingreso: junio-2009
Mensajes: 11
Antigüedad: 14 años, 11 meses
Puntos: 0
¿Coordenadas dentro de un polígono?

Hola,

tengo un polígono de coordenadas de Google Earth en un archivo .kml
recojo las coordenadas (tipo float) en una lista y quiero saber si un punto está dentro o fuera del polígono.

He encontrado en Internet el siguiente código:
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at mozilla.org
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Initial Owner of the Original Code is European Environment
# Agency (EEA). Portions created by Finsiel Romania are
# Copyright (C) European Environment Agency. All
# Rights Reserved.
#
# Authors:
# Alexandru Ghica, Finsiel Romania
# Bogdan Grama, Finsiel Romania
# Iulian Iuga, Finsiel Romania

Código Python:
Ver original
  1. # determine if a point is inside a given polygon or not
  2. # Polygon is a list of (x,y) pairs.
  3. def point_inside_polygon(x,y,l_poly):
  4.     n = len(l_poly)
  5.     inside =False
  6.  
  7.     j = 0
  8.     for i in range(n):
  9.         j+=1
  10.         if (j == n):
  11.              j = 0
  12.         if (l_poly[i][1] < y and l_poly[j][1] >= y or l_poly[j][1] < y and l_poly[i][1] >= y):
  13.             if (l_poly[i][0] + (y - l_poly[i][1]) / (l_poly[j][1] - l_poly[i][1]) * (l_poly[j][0] - l_poly[i][0]) < x):
  14.                 inside = not inside
  15.     return inside

y al llamar a la función con distintos datos:
Código Python:
Ver original
  1. if point_inside_polygon(latitud,longitud,L_bordes):
  2.     Inserta_waypoint(longitud,latitud,zoom,str(wp_name))

siempre me sale el mismo error:
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 312, in RunScript
exec codeObject in __main__.__dict__
File "C:\miquegordis\py\wp_poligono.py", line 390, in <module>
bucle_zoom(perimetro)
File "C:\miquegordis\py\wp_poligono.py", line 114, in bucle_zoom
bucle_poligono(L_h[zoom], L_v[zoom], L_range[zoom])
File "C:\miquegordis\py\wp_poligono.py", line 134, in bucle_poligono
if point_inside_polygon(latitud,longitud,L_bordes):
File "C:\miquegordis\py\wp_poligono.py", line 156, in point_inside_polygon
if (l_poly[i][1] < y and l_poly[j][1] >= y or l_poly[j][1] < y and l_poly[i][1] >= y):
TypeError: 'float' object is unsubscriptable

>>>

La función trabaja sobre coordenadas cartesianas (creo) y Google Earth con coordenadas geográficas; tengo duda si el error es por ese motivo.
No me importa que la precisión y fiabilidad sean escasas, el programa es para uso doméstico y es una prueba.

¿Alguien me puede echar una mano?
gracias anticipadas
saludos

Última edición por miquegordis; 08/06/2009 a las 02:37