Greeting all. Here is an example on the Leaflet main site of some GeoJson that is static. I want to make this more dynamic by say swapping out the density node data for something coming from a database (DB). Here is some code showing my attempt at mixing the static code with data retrieved from a DB done in PHP.
$jsonStr = "{\"type\":\"FeatureCollection\",\"features\":[";
foreach($data as $info){
if($counter == $count-1){
$jsonStr .= "
{\"type\":\"Feature\",\"id\":\"".$info["site_state"]."\",\"properties\":
{
\"name\":\"".$info["state_dscr"]."\",\"density\":".$info["coalesce"]."
}
}";
}else{
$jsonStr .= "
{\"type\":\"Feature\",\"id\":\"".$info["site_state"]."\",\"properties\":
{
\"name\":\"".$info["state_dscr"]."\",\"density\":".$info["coalesce"]."
}
},";
}
$counter++;
}
$jsonStr .= "]}";
$test = json_decode($jsonStr);
//Validate the JSON
if($test === NULL){
error_log("There is an issue with your json.");
}else{
header('Content-Type: application/json');
echo $jsonStr;
}
The above code is run via an D3 json() call like so:
d3.json("js/statesDataJson.php", function(vioData) { ...}
within another js file. And yes my json is missing the state coordinates, which is why i cannot reproduce the choropleth layer found in the example on the leaflet tutorial site (See below link).
This is my attempt at making this example found here
more dynamic. I thought i might be able to mix the data from two sources and using the "desnity" node data from one source and use the "geometry" node from the other to build the state layer. That is where i am having problems.
My initial idea is to set up a table in a DB for the geo JSON and continue with my above example of dynamically replicating this.
I am wondering if i am using a canon ball to kill a roach and there is there is a more efficient strategy or solution. Or is the database the only way to dynamically rebuild my json? For example would any advocate trying to parse the json file and edit the density node? Seems ... asinine but i am open to suggestions.
Thank you