0

I have a PHP endpoint/service that receives Lat/Long data from a 3rd party. The data is then processed in PHP code and saved into a MySQL database. This process works fine without any issues.

I need to run some spatial queries against the data, like does the source point fall inside a polygon (polygon source is GeoJSON), what is the distance from the point to a line (line source is GeoJSON) and distance to the end of a line (following the line segments).

I found TurfJS can cover all these queries, BUT TurfJS is obviously client side JavaScript functions, so I'm unable to call the Turf functions from PHP, retrieve the output values to save it in MySQL.

Any suggestions?

2 Answers 2

1

So I have gone the route of using Ajax/jQuery to post the JavaScript results to a PHP processing page.

In my page where I run the TurfJS functions, I added:

<html>
<head>
  <script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>
  <script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
</head>
<body>

<div id="result"></div>

<script type="text/javascript">

   //--- Turf Functions ---
   //...
   //...

   //--- Post values to processing page ---

   $.post('savetodb.php', {postVal1:val1, postVal2:val2},
   function(data){
     $('#result').html(data);
   });

</script>

</body>

In my processing page (savetodb.php), I have:

<?php

$saveVal1 = $_POST['postVal1'];
$saveVal2 = $_POST['postVal2'];

echo 'Value1: '.$saveVal1.' Value2: '.$saveVal2;

?>

Hope this post helps someone else some day!!

Pieter

0

Most of MySQL's spatial column types and functions are relatively simple. If you're only looking to do a few types of queries, this can be a straight forward approach. There's lots of stuff on Stack Exchange about shape intersection/bounds, like your point in polygon query. Here's one.

There's plenty about both querying by distance, and calculating (and returning) it in MySQL as well.

There are PHP packages that abstract some of the more complicated spatial database queries. Laravel MySQL Spatial is a package that integrates with the hugely popular PHP framework, Laravel.

There are also packages to do calculations outside the database, FYI.

A popular option would be GEOS PHP. It can be a little tricky to get configured though since it requires a PHP extension that must be installed with a package manager, like APT or YUM, or even compiled directly.

There's also a fork of it called funiq/geoPHP that doesn't require installing the GeoPHP extension, and offers quite a bit of its features stand-alone. But if you do install the PHP extension, it adds even more functionality and some speed optimizations.

All of these offer varying feature sets, but they all have at least basic abilities to do distance calculation, and bounds and intersects type math and/or database queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.