Skip to content

Commit

Permalink
Merge pull request #18 from laiconsulting/master
Browse files Browse the repository at this point in the history
meta query allow compare_key=LIKE
  • Loading branch information
nciske authored Oct 19, 2023
2 parents b48df5f + 8ce337c commit 853d2b6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
Binary file added .DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions sql/bearing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- Calculate bearing

DELIMITER $$
DROP FUNCTION IF EXISTS wp_bearing$$
DROP FUNCTION IF EXISTS wp_bevier_ctrlpts$$

CREATE FUNCTION `wp_bearing` (p1 POINT, p2 POINT) RETURNS FLOAT
NO SQL DETERMINISTIC
BEGIN
DECLARE lat1 FLOAT;
DECLARE lng1 FLOAT;
DECLARE lat2 FLOAT;
DECLARE lng2 FLOAT;
DECLARE geom1 GEOMETRY;
DECLARE geom2 GEOMETRY;
DECLARE dLng FLOAT;
DECLARE y FLOAT;
DECLARE x FLOAT;
DECLARE bearing FLOAT;

SET geom1 = wp_first_geom( p1 );
SET geom2 = wp_first_geom( p2 );
SET lat1 = RADIANS( ST_Y( geom1 ));
SET lng1 = RADIANS( ST_X( geom1 ));
SET lat2 = RADIANS( ST_Y( geom2 ));
SET lng2 = RADIANS( ST_X( geom2 ));

SET dLng = lng2 - lng1 ;

SET y = SIN( dLng ) * COS( lat2 );
SET x = ( COS( lat1 ) * SIN ( lat2 ) ) - ( SIN( lat1 ) * COS( lat2 ) * COS( dLng ) );
SET bearing = DEGREES( ATAN2( y, x ) );

RETURN bearing;
END$$


CREATE FUNCTION `wp_bevier_ctrlpts` (p1 POINT, p2 POINT, p3 POINT, factor FLOAT) RETURNS MULTIPOINT
NO SQL DETERMINISTIC
BEGIN
DECLARE len_s FLOAT;
DECLARE len_t FLOAT;
DECLARE bearing_s FLOAT;
DECLARE bearing_t FLOAT;
DECLARE dbearing FLOAT;
DECLARE bearing_m FLOAT;
DECLARE ctrlpt1_text TEXT;
DECLARE ctrlpt2_text TEXT;
DECLARE ctrlpt1 GEOMETRY;
DECLARE ctrlpt2 GEOMETRY;

SET len_s = wp_distance_point_real(p2 , p1, 1);
SET len_t = wp_distance_point_real(p2 , p3, 1);
SET bearing_s = (wp_bearing(p2, p1) + 180 + 360 ) % 360;
SET bearing_t = (wp_bearing(p2, p3) + 360 ) % 360;
SET dbearing = bearing_t - bearing_s ;

SET bearing_m = bearing_s + dbearing * len_s / (len_s + len_t);
SET ctrlpt1_text = wp_point_bearing_distance_coord_pair( p2, bearing_m, - len_s / factor, 1 );
SET ctrlpt2_text = wp_point_bearing_distance_coord_pair( p2, bearing_m, len_t / factor ,1 );
SET ctrlpt1 = ST_GeomFromText( CONCAT('POINT(',ctrlpt1_text,')'));
SET ctrlpt2 = ST_GeomFromText( CONCAT('POINT(',ctrlpt2_text,')'));

RETURN MULTIPOINT( ctrlpt1 , ctrlpt2 );
END$$

DELIMITER ;
19 changes: 10 additions & 9 deletions wp-geoquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,31 @@ private function handle_two_geom_bool_meta( &$clauses, $meta_query, $type, $prim
$meta_value = ( array_key_exists( 'value', $meta_query ) ? $meta_query['value'] : '' );
$geometry = WP_GeoUtil::metaval_to_geom( $meta_value, true );
if ( ! empty( $geometry ) ) {
$new_meta_value = "{$meta_query['compare']}( meta_value,ST_GeomFromText( %s, %d, 'axis-order=long-lat' ) )";
$new_meta_value = "{$meta_query['compare']}( meta_value,ST_GeomFromText( %s, %d ) )";
$new_meta_value = $wpdb->prepare( $new_meta_value, array( $geometry, WP_GeoUtil::get_srid() ) ); // @codingStandardsIgnoreLine

$std_query = "( $metatable.meta_key = %s AND CAST($metatable.meta_value AS $meta_type) = %s )";
$std_queries[] = $wpdb->prepare( $std_query, array( $meta_query['key'], $meta_query['value'] ) ); // @codingStandardsIgnoreLine
$std_query = " CAST($metatable.meta_value AS $meta_type) = %s ";
$std_queries[] = $wpdb->prepare( $std_query, array( $meta_query['value'] ) ); // @codingStandardsIgnoreLine

// In WP 4.6 and above CHAR values aren't cast anymore, so we do a replace on both versions
// so that we maintain 4.5.x capabilities too.
if ( 'CHAR' === $meta_type ) {
$std_query = "( $metatable.meta_key = %s AND $metatable.meta_value = %s )";
$std_queries[] = $wpdb->prepare( $std_query, array( $meta_query['key'], $meta_query['value'] ) ); // @codingStandardsIgnoreLine
$std_query = " $metatable.meta_value = %s ";
$std_queries[] = $wpdb->prepare( $std_query, array( $meta_query['value'] ) ); // @codingStandardsIgnoreLine
}
$geom_query = $wpdb->prepare( " $metatable.meta_value " ); // @codingStandardsIgnoreLine
} else {
// If we don't have a value, then our subquery gets written without parenthesis wraps.
// IDK why.
$new_meta_value = "{$meta_query['compare']}( meta_value )";

$std_query = " $metatable.meta_key = %s";
$std_queries[] = $wpdb->prepare( $std_query, array( $meta_query['key'] ) ); // @codingStandardsIgnoreLine
}

// Our geom_query will be against our aliased meta table so we just need to check for boolean true.
$geom_query = "( $metatable.meta_key = %s AND $metatable.meta_value )";
$geom_query = $wpdb->prepare( $geom_query, array( $meta_query['key'] ) ); // @codingStandardsIgnoreLine
// Our geom_query will be against our aliased meta table so we just need to check for boolean true.
$geom_query = "( $metatable.meta_key = %s AND $metatable.meta_value )";
$geom_query = $wpdb->prepare( $geom_query, array( $meta_query['key'] ) ); // @codingStandardsIgnoreLine
}

$this->make_join_spatial( $clauses,$meta_query,$type,$primary_table,$primary_id_column,$context, $metatable, $geotable, $id_column, $new_meta_value );

Expand Down
9 changes: 6 additions & 3 deletions wp-geoutil.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class WP_GeoUtil {
'ST_LineStringFromText',
'ST_LineStringFromWKB',
'ST_LongFromGeoHash',
'ST_MakeEnvelope',
'ST_NumGeometries',
'ST_NumInteriorRings',
'ST_NumPoints',
Expand Down Expand Up @@ -209,6 +210,8 @@ class WP_GeoUtil {
'Within',
'X',
'Y',
'WP_Bearing',
'WP_Bevier_CtrlPts',
'WP_Buffer_Point_M',
'WP_Buffer_Point_Mi',
'WP_Buffer_Point_Real',
Expand Down Expand Up @@ -702,7 +705,7 @@ public static function get_srid() {
* @param array $arguments The arguments for the function.
*/
public static function __callStatic( $name, $arguments ) {
if ( in_array( strtolower( $name ), self::get_capabilities(), true ) ) {
if ( in_array( strtolower( $name ),array_merge( self::get_capabilities(),array("linestring","point")), true ) ) {
return self::run_spatial_query( $name, $arguments );
}
}
Expand Down Expand Up @@ -736,7 +739,7 @@ private static function run_spatial_query( $name, $arguments = array() ) {
$maybe_geom = self::metaval_to_geom( $arg );
if ( false !== $maybe_geom ) {
$arguments[ $idx ] = $maybe_geom;
$q .= 'GeomCollFromText(%s)';
$q .= 'ST_GeomCollFromText(%s)';
} else {
$q .= '%s';
}
Expand Down Expand Up @@ -776,7 +779,7 @@ private static function run_spatial_query( $name, $arguments = array() ) {
\'0001000004\', -- multipoint
\'0001000005\', -- multiline
\'0001000006\' -- multipolygon
), false) , AsText( retval ), retval ) AS res FROM ( ' . $q . ' AS retval ) rq';
), false) , ST_AsText( retval ), retval ) AS res FROM ( ' . $q . ' AS retval ) rq';

$sql = $wpdb->prepare( $real_q, $arguments ); // @codingStandardsIgnoreLine

Expand Down

0 comments on commit 853d2b6

Please sign in to comment.