Skip to content

Commit 5b207e2

Browse files
committed
Factor RDP algorithm out into its own file(s)
1 parent 8d496ba commit 5b207e2

File tree

5 files changed

+100
-55
lines changed

5 files changed

+100
-55
lines changed

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ PHP_ARG_ENABLE(geospatial, whether to enable geospatial support,
55
[ --enable-geospatial Enable geospatial support])
66

77
if test "$PHP_GEOSPATIAL" != "no"; then
8-
PHP_NEW_EXTENSION(geospatial, geospatial.c geo_array.c geohash.c, $ext_shared)
8+
PHP_NEW_EXTENSION(geospatial, geospatial.c geo_array.c geohash.c rdp.c, $ext_shared)
99
fi

geospatial.c

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "geo_array.h"
3535
#include "geo_lat_long.h"
3636
#include "geohash.h"
37+
#include "rdp.h"
3738
#include "Zend/zend_exceptions.h"
3839
#include "ext/spl/spl_exceptions.h"
3940

@@ -804,60 +805,6 @@ int geojson_linestring_to_array(zval *line, geo_array **array)
804805
return 0;
805806
}
806807

807-
double rdp_find_perpendicular_distable(double pX, double pY, double p1X, double p1Y, double p2X, double p2Y)
808-
{
809-
double slope, intercept, result;
810-
811-
if (p1X == p2X) {
812-
return fabs(pX - p1X);
813-
} else {
814-
slope = (p2Y - p1Y) / (p2X - p1X);
815-
intercept = p1Y - (slope * p1X);
816-
result = fabs(slope * pX - pY + intercept) / sqrt(pow(slope, 2) + 1);
817-
return result;
818-
}
819-
}
820-
821-
void rdp_simplify(geo_array *points, double epsilon, int start, int end)
822-
{
823-
double firstX = points->x[start];
824-
double firstY = points->y[start];
825-
double lastX = points->x[end];
826-
double lastY = points->y[end];
827-
int index = -1;
828-
double dist = 0.0, current_dist;
829-
int i;
830-
831-
if (end - start < 2) {
832-
return;
833-
}
834-
835-
for (i = start + 1; i < end; i++) {
836-
if (!points->status[i]) {
837-
continue;
838-
}
839-
840-
current_dist = rdp_find_perpendicular_distable(points->x[i], points->y[i], firstX, firstY, lastX, lastY);
841-
842-
if (current_dist > dist) {
843-
dist = current_dist;
844-
index = i;
845-
}
846-
}
847-
848-
if (dist > epsilon) {
849-
rdp_simplify(points, epsilon, start, index);
850-
rdp_simplify(points, epsilon, index, end);
851-
852-
return;
853-
} else {
854-
for (i = start + 1; i < end; i++) {
855-
points->status[i] = 0;
856-
}
857-
return;
858-
}
859-
}
860-
861808
/* {{{ proto array rdp_simplify(array points, float epsilon)
862809
Simplifies a 2D dimensional line according to the Ramer-Douglas-Peucker algorithm */
863810
PHP_FUNCTION(rdp_simplify)

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
<file name="geohash.h" role="src" />
5353
<file name="php_geospatial_arginfo.h" role="src" />
5454
<file name="php_geospatial.h" role="src" />
55+
<file name="rdp.h" role="src" />
5556
<file name="geo_array.c" role="src" />
5657
<file name="geohash.c" role="src" />
5758
<file name="geospatial.c" role="src" />
59+
<file name="rdp.c" role="src" />
5860
</dir> <!-- / -->
5961
</contents>
6062
<dependencies>

rdp.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2024 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
#include <math.h>
19+
20+
#include "geo_array.h"
21+
22+
static double rdp_find_perpendicular_distable(double pX, double pY, double p1X, double p1Y, double p2X, double p2Y)
23+
{
24+
double slope, intercept, result;
25+
26+
if (p1X == p2X) {
27+
return fabs(pX - p1X);
28+
} else {
29+
slope = (p2Y - p1Y) / (p2X - p1X);
30+
intercept = p1Y - (slope * p1X);
31+
result = fabs(slope * pX - pY + intercept) / sqrt(pow(slope, 2) + 1);
32+
return result;
33+
}
34+
}
35+
36+
void rdp_simplify(geo_array *points, double epsilon, int start, int end)
37+
{
38+
double firstX = points->x[start];
39+
double firstY = points->y[start];
40+
double lastX = points->x[end];
41+
double lastY = points->y[end];
42+
int index = -1;
43+
double dist = 0.0, current_dist;
44+
int i;
45+
46+
if (end - start < 2) {
47+
return;
48+
}
49+
50+
for (i = start + 1; i < end; i++) {
51+
if (!points->status[i]) {
52+
continue;
53+
}
54+
55+
current_dist = rdp_find_perpendicular_distable(points->x[i], points->y[i], firstX, firstY, lastX, lastY);
56+
57+
if (current_dist > dist) {
58+
dist = current_dist;
59+
index = i;
60+
}
61+
}
62+
63+
if (dist > epsilon) {
64+
rdp_simplify(points, epsilon, start, index);
65+
rdp_simplify(points, epsilon, index, end);
66+
67+
return;
68+
} else {
69+
for (i = start + 1; i < end; i++) {
70+
points->status[i] = 0;
71+
}
72+
return;
73+
}
74+
}
75+

rdp.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2024 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
#include "geo_array.h"
19+
20+
void rdp_simplify(geo_array *points, double epsilon, int start, int end);
21+

0 commit comments

Comments
 (0)