How can I find the middle of a HERE Routing API route? - php

by doing a GET-Request, I am receiving routing informations from the HERE Routing API:
https://router.hereapi.com/v8/routes?apikey=MY_API_KEY&destination=52.530394,13.400683&origin=52.530728,13.383833&return=polyline,travelSummary&transportMode=truck&&vehicle[speedCap]=30&spans=names,length,duration,speedLimit
Now I want to find the coordinates for example in the middle of the route with respect to the total time. So I the example below, the total duration is 274 seconds. How can I find out, on which position I will be after 137 seconds? (In real application these times are much longer. Here, for simplicity and for a small JSON file size, I have chosen only a short distance)
First, I thought of adding starting and ending coordinates of the spans, however it seems not to be possible with the API.
Second, I thought of using the polyline. From that I receive a lot of coordinates, however I don't see a possiblity to connect one of these coordinates to a certain duration of travel.
Is there any way how I can get the information I am looking for with the HERE Routing API or with any PHP calculation?
{
"routes": [
{
"id": "90be4eb8-d0ba-47f8-9954-9be444576a17",
"sections": [
{
"id": "bfd32e45-662b-4b7e-a297-21eeee09dd68",
"type": "vehicle",
"departure": {
"time": "2021-12-11T23:42:04+01:00",
"place": {
"type": "place",
"location": {
"lat": 52.5307744,
"lng": 13.3838015
},
"originalLocation": {
"lat": 52.5307279,
"lng": 13.383833
}
}
},
"arrival": {
"time": "2021-12-11T23:46:38+01:00",
"place": {
"type": "place",
"location": {
"lat": 52.5303982,
"lng": 13.4006967
},
"originalLocation": {
"lat": 52.5303939,
"lng": 13.4006829
}
}
},
"travelSummary": {
"duration": 274,
"length": 1338,
"baseDuration": 264
},
"polyline": "BGslnmkDyn8wZ8CmL4Iof0F0U8BoGsEoQwCsJsEkSoBoG8BsJsE0U8BgK8BoLoB4IoB0KoBoLoBkNwC8a8B0UoB0UoBkNsEgtBkDsd8BsTkDgZsEgtB4D0jBgFwvBoG46B8B8QwCoV8BwMgFgtBUwHkD8akDgeU4NoB4XAkIoB0ZoB8pBU0K8Boa8B0PkDkS7GkD3I0F3DwC7foa7G0Fzeoaze0ZvTiQ",
"spans": [
{
"offset": 0,
"names": [
{
"value": "Invalidenstraße",
"language": "de"
}
],
"length": 189,
"duration": 31,
"speedLimit": 13.8888893
},
{
"offset": 11,
"names": [
{
"value": "Invalidenstraße",
"language": "de"
}
],
"length": 872,
"duration": 184,
"speedLimit": 8.333334
},
{
"offset": 44,
"names": [
{
"value": "Brunnenstraße",
"language": "de"
}
],
"length": 277,
"duration": 59,
"speedLimit": 8.333334
}
],
"transport": {
"mode": "truck"
}
}
]
}
]
}

Using the information in the spans object is definitely the way to go. What you need is to break up the spans into as many pieces as possible. You can do that by adding these values to the parameter in your request:
&spans=duration,length,segmentId,names,speedLimit,dynamicSpeedInfo,baseDuration,typicalDuration,segmentRef
You'll see that the response includes a list of spans identified by the offset attribute, which tells you what coordinate in your polyline that span refers to. This means that you want to know what is the offset (coordinate index) where the sum of span durations is 137.
This procedure will get you the best approximation to the middle of the route relative to travel time:
Loop through the list of spans and sum the value in the duration attribute; the loop should stop when the sum is equal or greater than the desired duration (137 in your example).
Get the value of the offset attribute, and add 1.
Decode your polyline, and get the coordinates at the index that is equal to the number you got in step 2 (offset + 1).
For the route in your example, the span that meets the condition in step 1 is offset=31, so you're interested in the coordinates at index 32 from your polyline.

Related

How to get the insights per campaign for multiple campaigns that belong to a specific ad account in a single call

We want to get the insights per campaign for multiple campaigns that belong to a specific ad account in a single call.
I am using the following http call and it returns me the correct insights(reach, impressions and clicks) and other data per campaign for the last 30 days only.
https://graph.facebook.com/v3.3/<ad_account>/campaigns?fields=name,status,insights{reach,impressions,clicks}&access_token=<access_token>
How can I use the parameter date_preset so that I will be able to get the insights(reach, impressions and clicks) and other data per campaign for lifetime?
If there is any other way to get the above insights for lifetime without using the date_preset please do not hesitate to advise me how to get them.
We wanted to let you know that our app is written in php.
Because insights is an edge of the campaign object you can apply some filter parameters via dot on that edge, like this:
https://graph.facebook.com/v3.3/<ad_account>/campaigns?fields=name,status,insights.date_preset(lifetime){reach,impressions,clicks}&access_token=<access_token>
Or, if you want to check Insights with the time breakdown (three months, for example) and also take a look on the lifetime summary you can do it like this:
https://graph.facebook.com/v3.3/<ad_account>/campaigns?fields=name,status,insights.default_summary(true).limit(3).date_preset(lifetime).time_increment(monthly){reach,impressions,clicks}&access_token=<access_token>
{
"name": "Campaign Name",
"status": "Campaign Status",
"insights": {
"data": [
{
"reach": "149599",
"impressions": "291917",
"clicks": "13517",
"date_start": "2019-01-11",
"date_stop": "2019-01-31"
},
{
"reach": "265556",
"impressions": "456458",
"clicks": "7915",
"date_start": "2019-02-01",
"date_stop": "2019-02-28"
},
{
"reach": "233641",
"impressions": "331600",
"clicks": "4671",
"date_start": "2019-03-01",
"date_stop": "2019-03-31"
}
],
"paging": {
"cursors": {
"before": "BFR",
"after": "AFT"
},
"next": "next link"
},
"summary": {
"reach": "660772",
"impressions": "1486924",
"clicks": "32484",
"date_start": "2019-01-11",
"date_stop": "2019-05-06"
}
},
"id": "000"
}
https://developers.facebook.com/docs/marketing-api/insights/parameters#param

How to show specific data on modal without unique value (id) in php

I have a data set coming from an API. Data does not have an id attribute in it. When we want to show specific data on the base of its id in modal we declare a data-id attribute in html but if we do not have any id associated to data. How can we show the contents of it on modal?
See the data, I am showing name and have more-info button for modal. what should i do, when someone clicks on more-info button. It shows specific content?
There is also a next url in the data how can i paginate it in php?
{
"count": 37,
"next": "http://some-url/api/stars/?page=2",
"previous": null,
"results": [
{
"name": "Sentinel-class landing craft",
"model": "Sentinel-class landing craft",
"manufacturer": "Sienar Fleet Systems, Cyngus Spaceworks",
"cost_in_credits": "240000",
"length": "38",
"max_atmosphering_speed": "1000",
"crew": "5",
"passengers": "75",
"cargo_capacity": "180000",
"consumables": "1 month",
"hyperdrive_rating": "1.0",
"MGLT": "70",
"starship_class": "landing craft",
"pilots": [],
"films": [
"http://some-url/api/films/1/"
],
"created": "2014-12-10T15:48:00.586000Z",
"edited": "2014-12-22T17:35:44.431407Z",
"url": "http://some-url/api/stars/5/"
},
{
"name": "Death Star",
"model": "DS-1 Orbital Battle Station",
"manufacturer": "Imperial Department of Military Research, Sienar Fleet Systems",
"cost_in_credits": "1000000000000",
"length": "120000",
"max_atmosphering_speed": "n/a",
"crew": "342953",
"passengers": "843342",
"cargo_capacity": "1000000000000",
"consumables": "3 years",
"hyperdrive_rating": "4.0",
"MGLT": "10",
"starship_class": "Deep Space Mobile Battlestation",
"pilots": [],
"films": [
"http://some-url.co/api/films/1/"
],
"created": "2014-12-10T16:36:50.509000Z",
"edited": "2014-12-22T17:35:44.452589Z",
"url": "http://some-url.co/api/starships/9/"
},
$arrayData = json_decode($jsonData);
$i=0;
foreach($arrayData->results as $data)
{
$arrayData->results[$i]->name;
$arrayData->results[$i]->id=$i;
$i++;
}

php json parsing objects

I have a JSON file that I need to parse. I'm new to php and trying to figure out how to correctly parse the object. My question is: How do I parse the objects of an object that have different names
One object has the name Thresh the next object in the list has the name Aatrox the name of the top level object is data
This is what the JSON looks like. I can access the information if I know the name of the object $champion = $jfo->data->Thresh; but I don't want to have to type in all the names of the champions. Is there an easy way to obtain all the separate objects without knowing the names? Maybe regex?
"data": {
"Thresh": {
"id": 412,
"key": "Thresh",
"name": "Thresh",
"title": "the Chain Warden",
"image": {
"full": "Thresh.png",
"sprite": "champion3.png",
"group": "champion",
"x": 336,
"y": 0,
"w": 48,
"h": 48
},
"Aatrox": {
"id": 266,
"key": "Aatrox",
"name": "Aatrox",
"title": "the Darkin Blade",
"image": {
"full": "Aatrox.png",
"sprite": "champion0.png",
"group": "champion",
"x": 0,
"y": 0,
"w": 48,
"h": 48
},
If you want to go through each champion, I'd recommend using a foreach loop in PHP. You can use it as such:
foreach($json->data as $champion)
{
// Do something
}

How to filter distinct data in CloudSearch:AWS?

I have a query in aws cloudsearch. I did the following things
1) Created domain
2) uploaded the data & created indexing
I have data fields like : user_id, user_name, user_details, etc
My objective is to get the grouped/distinct data of particular field & its total count. In Cloudsearch Group by / Distinct key words not supported. So, I went through the cloudsearch documentation & done it by adding facet.user_id={} in my query string.
But I need user_name field data along with user_id and count.** Please update me regarding this.
Here is my full query : ?q="Tamil Selvan"&facet.user_id={}
Here is my query result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"count": 213
}, {
"value": "182",
"count": 197
}]
}
}
}
My expected result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"user_name":"Tamil Selvan",
"count": 213
}, {
"value": "182",
"user_name":"Tamil Selvi",
"count": 197
}]
}
}
}
The proper solution would be to look up the user_names for the user_id facet values from your datastore (which CloudSearch is not, or at least should not be).
CloudSearch is a search solution; you shouldn't be trying to ask it which user_name belongs to some user_id, as that's a question for your data store.

mongodb geolocation indexing and quering

I have following mongo document structure.
{
"geo": {
"0": {
"deliveryArea": {
"0": {
"lat": 50.449234773334,
"lng": 30.52300029031
},
"1": {
"lat": 50.449234773334,
"lng": 30.52542980606
},
"2": {
"lat": 50.45154573136,
"lng": 30.52542980606
},
"3": {
"lat": 50.45154573136,
"lng": 30.52300029031
}
},
"title": "Kiev, ....",
"coords": {
"lat": "50.4501",
"lngt": "30.523400000000038"
},
"wholeCityDelivery": "false"
}
Questions:
How to ensure_index for every element in deliveryArea [lat,lng] for geolocationg?
How to build query to find documents with the point N[lat,lng] in(belongs to) polygon deliveryArea.$
If you can do PHP - please)
Thanks!
First of all, you need to change your data structure. MongoDB wants "longitude, lattitude" and it doesn't care which names you give to the fields—in general, I would use them without the field names. You also need to store your document without the "0" keys
So like:
{
"geo": {
"deliveryArea": [
[ 30.52300029031, 50.449234773334 ],
]
}
}
Then you need to set a 2D index on "geo.deliveryArea":
$collection->ensureIndex( array( "geo.deliveryArea" => "2d" ) );
Information on how to build queries can be at http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-BoundsQueries However, in your case you want to find a delivery area for a point, instead of checking whether stored points fit in a given area, and MongoDB can not do that directly as its 2d index can only store points.
Please file a feature request at http://jira.mongodb.org

Categories