Excel - Algoritmo del punto en un polígono en VBA Excel

 
Vista:

Algoritmo del punto en un polígono en VBA Excel

Publicado por dave (1 intervención) el 21/02/2020 14:17:33
Que tal trabajo con polígonos y coordenadas en mapas tengo un algoritmo que busca puntos en polígonos pero esta realizado en php mi pregunta seria si alguien realizo este tipo de trabajo pero en VBA Excel o solamente en excel ahora debo trabajar en excel con plantillas alguien sabe si se puede realizar, agradecería mucho un poco de ayuda.
Gracias...

este es el código con el q trabajo realizado en php acá introduzco el polígono en un array esta es la función con la cual llamo al algoritmo realizado en otro archivo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
include_once "Poligonocbba.php";
//CP3 comercial de primera
 
$avenidas = array(
	$polygon1 = array("-17.391161092837038 -66.153487523247634","-17.392071363549764 -66.153268692381673","-17.393003047705985 -66.158290580203186","-17.399182028254931 -66.157272175019301","-17.399029430296597 -66.158155915054934","-17.393098089932192 -66.159237444527108","-17.392981629732706 -66.158328454776182","-17.3921891629589 -66.158528114561022","-17.391883955244136 -66.156508137337454","-17.391161092837038 -66.153487523247634"),
 
	$polygon2 = array("-17.407452701789996 -66.156796312399209","-17.407452701784493 -66.156796312308387","-17.402104462394735 -66.15758660444358","-17.401986031910788 -66.156975242399014","-17.401773295478243 -66.154987166579019","-17.401839090281506 -66.150845533477579","-17.40242023579896 -66.150590851947527","-17.403445160811142 -66.150734802358699","-17.404290455465997 -66.150624071273157","-17.404438381628697 -66.150624071273157","-17.405241200748925 -66.150453043144907","-17.404850048120824 -66.154268464365003","-17.408442268789138 -66.154580284832022","-17.408729777116932 -66.155591073649305","-17.407824189589096 -66.156783737834033","-17.407452701789996 -66.156796312399209"),
 
	$polygon3 = array("-17.397948383782627 -66.153948310766239","-17.39779047337014 -66.152845100826042","-17.39768797395887 -66.152074357096282","-17.398173869067392 -66.152022970951904","-17.398182784561914 -66.151186778238838","-17.398789037169141 -66.151093348885425","-17.398784579436718 -66.151910855727806","-17.399571349201992 -66.151824632094389","-17.399501167211202 -66.153654121519509","-17.398150191742676 -66.153920711675639","-17.397948383782627 -66.153948310766239"),
 
	$polygon4 = array("-17.377820547118542 -66.161451984383007","-17.377699116517817 -66.160808308683968","-17.380477714216305 -66.160044879366509","-17.380577714375502 -66.160696039666703","-17.377820547118542 -66.161451984383007"),
 
	$polygon5 = array("-17.372374525601852 -66.157297523200313","-17.373401086631095 -66.157315044977622","-17.373080139281758 -66.162054979077539","-17.37254610418552 -66.16219911626753","-17.372476961563432 -66.163289852373595","-17.373114507504578 -66.163070575405484","-17.373207718635896 -66.16346351238461","-17.37198405111441 -66.163913232040201","-17.372268221610213 -66.159098605789325","-17.372719388010253 -66.159136424438884","-17.372774018613192 -66.158657019007322","-17.372313987826331 -66.158619701484511","-17.372374525601852 -66.157297523200313")
);
 
function inside($lat, $lng){
$pointLocation = new pointLocation();
$point = ("$lat $lng");
global $avenidas;
$n_avenida=0;
$verificar="outside";
 
	for($i = 0; $i < count($avenidas); ++$i) {
		if($verificar=="outside"){
			$verificar = $pointLocation->pointInPolygon($point, $avenidas[$i]);
		}else{
			$n_avenida = $i;
			break 1;}
 
	}
	return array ($verificar,$n_avenida);
}


acá esta el algoritmo de búsqueda el cual me devuelve el estado si las coordenadas se encuentran dentro o fuera del código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class pointLocation {
    var $pointOnVertex = true; // Checar si el punto se encuentra exactamente en uno de los vértices?
 
    function pointLocation() {
    }
 
        function pointInPolygon($point, $polygon, $pointOnVertex = true) {
        $this->pointOnVertex = $pointOnVertex;
 
        // Transformar la cadena de coordenadas en matrices con valores "x" e "y"
        $point = $this->pointStringToCoordinates($point);
        $vertices = array();
        foreach ($polygon as $vertex) {
            $vertices[] = $this->pointStringToCoordinates($vertex);
        }
 
        // Checar si el punto se encuentra exactamente en un vértice
        if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
            return "inside";
            /*return "vertex";*/
        }
 
        // Checar si el punto está adentro del poligono o en el borde
        $intersections = 0;
        $vertices_count = count($vertices);
 
        for ($i=1; $i < $vertices_count; $i++) {
            $vertex1 = $vertices[$i-1];
            $vertex2 = $vertices[$i];
            if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Checar si el punto está en un segmento horizontal
                return "inside";
                /*return "boundary";*/
            }
            if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {
                $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
                if ($xinters == $point['x']) { // Checar si el punto está en un segmento (otro que horizontal)
                    return "inside";
                    /*return "boundary";*/
                }
                if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
                    $intersections++;
                }
            }
        }
        // Si el número de intersecciones es impar, el punto está dentro del poligono.
        if ($intersections % 2 != 0) {
            return "inside";
        } else {
            return "outside";
        }
    }
 
    function pointOnVertex($point, $vertices) {
        foreach($vertices as $vertex) {
            if ($point == $vertex) {
                return true;
            }
        }
 
    }
 
    function pointStringToCoordinates($pointString) {
        $coordinates = explode(" ", $pointString);
        return array("x" => $coordinates[0], "y" => $coordinates[1]);
    }
 
}

Gracias...
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder