points stored in geoJSON do not show up on map - php

I've got a geoJSON file consisting of:
{
"type": "FeautureCollection",
"features": [
{
"type": "Feature",
"id": "1",
"geometry": {
"type": "Point",
"coordinates": [
5.709531,
50.855802
]
}
},
{
"type": "Feature",
"id": "2",
"geometry": {
"type": "Point",
"coordinates": [
5.709426,
50.855798
]
}
}
]
}
This JSON file is constructed in a php script:
$ret = array();
$ret['type'] = "FeautureCollection";
$ret['features'] = array();
$f = array();
$f['type'] = 'Feature';
$f['id'] = $p['id'];
$f['geometry'] = array();
$f['geometry']['type'] = 'Point';
$f['geometry']['coordinates'] = array(floatval($p['lat']), floatval($p['lon']));
$ret['features'][] = $f;
echo json_encode($ret);
When i try to load it through the polymaps.org framework none of the points are displayed.
besides the standard stuff to load the map I use this javascript to parse the geoJSON.
map.add(po.geoJson()
.url('geojson/c.geo.php')
.id('test')
);
The CSS is the following, so that the points should look like a red-circle:
#test {
fill: lightcoral;
fill-opacity: .5;
stroke: brown;
}
The strange thing is, that when I alter the geojson file pointer to one constructed by a GIS application it works. So I bet my geoJSON is corrupted. But when I run it through http://jsonlint.com/ it validates.
Does anybody know how this is possible?

Not sure, but I think I have ever face the same problem with another framework:http://leaflet.cloudmade.com/
In my case, it is a matter of view position (e.g: you feature is on America, but your map show Africa). Also in cloudmade-leaflet, they use latitude-longitude instead of longitude-latitude.
You can check if your map view the correct area.

Related

How to delete JSON Element in PHP?

I have a JSON object key element that i want to delete.
Lets say i want to delete the element of everything inside 'v8fe3m'
I tried using unset and delete. Nothing seems work unless my syntax is wrong.
{
"projects": {
"587ye4": {
"name": "abc",
"ip": "zz",
"loc": "azz"
},
"v8fe3m": {
"name": "japan",
"ip": "aaa",
"loc": "123",
"backups": {
"HELLO_1595524710053": {
"ts": 1595524710053,
"name": "HELLO",
"size": 770641
},
"HELLO_1595524717330": {
"ts": 1595524717330,
"name": "HELLO",
"size": 770641
},
"HELLO_1595524717558": {
"ts": 1595524717558,
"name": "HELLO",
"size": 770698
}
}
},
"x0190a": {
"name": "dubai",
"ip": "101",
"loc": "UAE"
}
}
}
$user_token = $_SESSION["userToken"];
$user_projects_json = read_json($GLOBALS['URL_JSON'] . "$user_token" . "_projects" .".json");
$projectKey = $_REQUEST['dataKey'];
$projectKey = trim($projectKey," ");
//v8fe3m
$backups = $user_projects_json['projects'][$projectKey];
unset($backups);
Since PHP is a strange language, it copies objects. Therefore $backups is a copy of $user_projects_json['projects'][$projectKey].
$backups = $user_projects_json['projects'][$projectKey];
unset($backups); //You are unsetting a very new object.
You can do
$backups = &$user_projects_json['projects'][$projectKey];
unset($backups); //You are unsetting a reference that refers to your object.
This way $backups are referring to your original object, thus unsetting what you need. I saw someone did it in the comments, but there were no explanation, unsetting without creating a new object.
First use $arrayFromJson = json_decode($yourJsonObject, true);, then you can use standard PHP array functions such as:
unset($arrayFromJson['v8fe3m']);

Laravel Query to select list entry in JSON field

I'm storing dynamic data in a MySQL JSON field using Laravel v6.11.0, nova v2.9.3 and nova-flexible-content v0.1.13. The data stored looks similar to this:
[
{
"layout": "source",
"key": "5ce8e0a877487fe5",
"attributes": {
"value": "342",
"unit": "USD",
"language": "en",
"url": "http:\\/\\/google.com",
"authority": "google.com",
"entry_date": "2020-01-21",
"date": "2019-12-21"
}
},
{
"layout": "source",
"key": "a82393ce016e8c14",
"attributes": {
"value": "444",
"unit": "USD",
"language": "en",
"entry_date": "2020-01-21",
"url": "https:\\/\\/google.com",
"authority": "TEST",
"date": "2020-01-20"
}
}
]
I was wondering if it's possible to build a Laravel Query to select the second entry based on the authority entry? Criteria are:
url should contain google.com AND
authority shouldn't be google.com
I've found https://laravel.com/docs/5.8/queries#json-where-clauses, but am struggling to put the right query together. Maybe someone could give me some pointers on how to do it? Thank you
You may refer to this discussion on Laracast
$data = App\YourModel::whereRaw('JSON_CONTAINS(body->"$[*].attributes.url", "\"https://google.com\"")')
->orWhereRaw('JSON_CONTAINS(body->"$[*].id", "\"http://google.com\"")')
->whereRaw('not JSON_CONTAINS(body->"$[*].attributes.authority", "\"google.com\"")')
->get();
foreach ($data as $key => $row) {
$bodyArr = json_decode($row->body);
foreach ($bodyArr as $item) {
if ((preg_match("/(http:\/\/www\.|https?:\/\/google.com)/i",$item->attributes->url)) && (strpos($item->attributes->authority, 'google.com') === false)) {
dump($item);
// YOUR CODE GOES HERE
}
}
}

How to modify large json structure for Google Map using PHP

I have a Google Map using the Google Map API drawing polylines from a .json file. A portion of the file structure can be seen below, it just repeats for each polyine feature. It was created in GIS and converted to a .geojson and then json.
I am looking to allow a user to modify specific properties of a feature through a mapping interface. I have the iterface and user interactions all built out but am having trouble modifying the .json file.
I have tried the following:
PHP (Note: I am hard-coding some values to test/get it to work)
$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['UID'] == 25301) {
$data[$key]['RENDER_CL'] = "99";
}
}
$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);
Errors:
Warning: Illegal string offset 'UID' on line...
Notice: Undefined index: UID on line...
Thoughts:
I feel I am not going deep enough in to the json "tree" to access the properties. I have not modified json data before so I am a bit lost. Should I somehow be going deeper into the structure? UID is unique for each item. Thanks.
JSON file
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25301
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
}
}, {
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
},
You are accessing your object's property incorrectly. the correct way would be $entry['properties']['UID'].
Example
<?php
$json =
'{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
}';
$obj = json_decode($json, true);
print $obj['properties']['UID'];

Accessing array inside array

I'm working on my task right now, which accessing specific array when it is called by front end.
My example data is like this
{
"data": [
{
"name": "Jon Snow",
"id": "01-0001",
},
{
"name": "Robert Stark",
"id": "01-0002"
},
{
"name": "Sansa Stark",
"id": "01-002333"
},
{
"name": "Arya Stark",
"id": "01-00012"
},
{
"name": "Bran Stark",
"id": "01-0003"
},
{
"name": "Rickon Stark",
"id": "01-0005"
}
]
}
* In my front end I have this code *
selectedEmployee: any=null;
setActiveEmployee(employee: any) {
this.selectedEmployee = employee;
let myJSON = JSON.stringify(this.selectedEmployee);
this.perEmpLateEarly();
}
Whenever I choose the employee i get the id of that employee.
So in this, if i choose "id": "01-0001" it will return the first array and it will keep the rest, and if I choose "id": "01-0002" it will return the second one and will keep the rest and so on. How can I do that in Php?
Thanks in advance
You will do a GET/POST HTTP request from frontend, which would look something like this in
your_backend_page.php?id=<ID HERE>
Then the "your_backend_page.php" would look like as follows:
$list = { "data": [ { .... }, { ... }] } // this is the array you have
$idFromFrontEnd = $_GET["id"];
foreach ($list["data"] as $item) { // iterate over all items
if ($item["id"] == $idFromFrontEnd) { // check if id matches
echo json_decode($item); // if matches, print this item
}
}
Please note this approach is okay if you have a small number of items. For a larger list, you might want to have a database, where you can use sql to select.

Pushing object to nested JSON using PHP?

In the following JSON object, I have two dummy products and a nested group of reviews that are siblings to one another:
product.json
[
{
"name": "Dodecahedron",
"price": 2.95,
"description": "This gem is awesome and has 10 sides.",
"images": [
{
"full": "dodecahedron-01-full.jpg",
"thumb": "dodecahedron-01-thumb.jpg"
}
],
"reviews": [
{
"stars": 5,
"body": "I love this product!",
"author": "joe#thomas.com"
},
{
"stars": 1,
"body": "This product sucks",
"author": "tim#hater.com"
}
]
},
{
"name": "Hectahedron",
"price": 8.95,
"description": "Wonderful 6-sided gem that will please all.",
"images": [
{
"full": "hectahedron-01-full.jpg",
"thumb": "hectahedron-01-thumb.jpg"
}
],
"reviews": [
{
"stars": 4,
"body": "product is awesome, seriously!",
"author": "james#crazy.com"
},
{
"stars": 2,
"body": "Seriously sucks, would give 0 if i could",
"author": "john#hater.com"
}
]
}
]
I am using AngularJS to send the newly created JS review object from an HTML form to PHP. But how in PHP do you push this review data to become a sibling IN "reviews" AND target the exact product it should be in? I'm very new to PHP and would greatly appreciate your guidance!
If I understand what you are asking, here you need to use the json_decode() function:
$productsRreviews = json_decode($_POST['reviews'], true);
This will give you a PHP associative array that you can process to do whatever you need to do. For instance:
foreach ($productsReviews as $productReviews) {
$name = $productReviews['name'];
$price = $productReviews['price'];
$reviews = $productReviews['reviews'];
foreach ($reviews as $review) {
$stars = $review['stars'];
...
}
}
Hope that helps!

Categories