TurfJS + PHP + MySQL

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

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?

profilepic.png
manpreet 2 years ago

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:



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



<div id="result">div>

 type="text/javascript">

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

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

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



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 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community