Socrata map results in $http request, Javascript - javascript

I am trying to use this dataset to determine which district boundary an address (passed into the API call) falls within.
The endpoint returns an array of objects for each district or council. The polygon is found within the "the_geom" property, with 2 properties - type and coordinates. I have tried using $where, but I get errors.
[
{
"comments": "Inaugurated 2015-06-22",
"council": "1",
"councilper": "Scott Griggs",
"district": "1",
"objectid": "1",
"shape_area": "343352603.892",
"shape_leng": "88541.3042539",
"the_geom": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-96.80995700065864,
32.77138899977414
],
[
-96.80969800043205,
32.77121999997131
],
[ ...
I tried to use the query below, but it gave me an error:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=within_polygon(the_geom, 'MULTIPOLYGON (((-96.800270, 32.779091)))')
This is the page reference page - https://www.dallasopendata.com/Geography-Boundaries/Adopted-Council-Districts/6dcw-hhpj
And this is the endpoint- https://www.dallasopendata.com/resource/dgxr-hmze.json
any help would be greatly appreciated.

I suspect you're the developer who popped into our IRC channel, but I'll answer here too!
You're pretty close here! What you want to do here is use the intersects(...) SoQL function with a Well Known Text (WKT) POINT.
Here's an example that works for your use case:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=intersects(the_geom,%20%27POINT%20(-96.7994007%2032.775765)%27)

Related

Limit length array-type of property in Loopback 4 query?

I've been trying this new framework Loopback 4 and its awesome but I dont know to which point is flexible,I'm having the following model on the database:
{
"id": "string",
"lastUpdate": "2020-10-01T18:10:46.306Z",
"name": "string",
"logo": "string",
"data": [
{}
]
}
And what I'm trying there is to make a query that returns me the data, but as is an array, it has a lot of data and I would like to paginate it, so I thought on limiting the query. I've achieved a query to look like the following:
{
"offset": 0,
"limit": 10,
"skip": 0,
"where": {
"name": {"eq":"BengalaSpain"}
},
"fields": {
"data": true
}
}
I'm trying to limit the data property to 10, but of course, this dosnt affects the property itself, just the wrapper object around it. Is there any way to achieve what im trying?
Thanks in advance guys!
LoopBack 4 filters apply at a Repository level as these constraints are passed to the ORM datasource connectors to be converted into their respective native queries (e.g. TOP 10 for SQL Server).
A possible solution is to link the data field into a Relation. Relations essentially create nested Repositories (e.g. hasManyRepository), hence are able to meet the requirement of isolating data into its own Repository.
To quickly create a relation, remove the property from the Model and re-create it using lb4 relation command.
From there, it would be possible to take advantage of the now-enabled InclusionResolver and write use query:
{
"where": {
"name": {"eq":"BengalaSpain"}
},
"fields": {
"data": true
},
"include": [
{
"relation": "<relation name here>",
"scope": {
"limit": 10
}
}
]
}
A side-effect is the separation of data into its own table. However, this should be done irregardless due to database normalization.

How to convert JSON to GeoJSON

I am very new in my learnings of javascript and my rudimentary knowledge has hit a wall. I have setup a Leaflet map which I am wishing to plot divIcon based markers from cords on it from JSON. Through my countless research of trying to get it to work. I learned why my JSON file wasn't working even though I confirmed in the console it was being read. I learned Leaflet prefers it to be in GeoJSON. So I spent several more hours researching how to convert this. Most of my findings were outdated and did not work anymore or did not apply to me. This is what I did try through my rigorous research.
To start off I have set a variable for the path to my test JSON file as defined like this.
var jsonData = "./data/tracking.json";
In my attempt to convert the JSON to GeoJSON I tried this.
var outGeoJson = {}
outGeoJson['properties'] = jsonData
outGeoJson['type']= "Feature"
outGeoJson['geometry']= {"type": "Point", "coordinates":
[jsonData['lat'], jsonData['lon']]}
console.log(outGeoJson)
Checked the console and found the coordinates in the array from JSON file are undefined.
My search for a reason why this was coming up undefined fell short. My theory here is maybe because the JSON has a key of positions prior to the array and the fact it is an array. I continue to search for a valid solution that could possibly handle this issue. I tried this solution next.
var geojson = {
type: "FeatureCollection",
features: [],
};
for (i = 0; i < jsonData.positions.length; i++) {
if (window.CP.shouldStopExecution(1)) {
break;
}
geojson.features.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [jsonData.positions[i].longitude, jsonData.positions[i].latitude]
},
"properties": {
"report_at": jsonData.positions[i].report_at,
"lat": jsonData.positions[i].lat,
"lon": jsonData.positions[i].lon,
"dir": jsonData.positions[i].dir,
"first": jsonData.positions[i].first,
"last": jsonData.positions[i].last
}
});
}
window.CP.exitedLoop(1);
console.log(geojson)
This solution gave me an error in the console of Uncaught TypeError: Cannot read property 'length' of undefined.
Attempted to troubleshoot that solution for several more hours and that has fallen short as well. Here is a sample of the test JSON file I am working with.
{
"positions": [
{
"report_at": "2015-01-21 21:00:08",
"lat": "38.9080658",
"lon": "-77.0030365",
"elev": "0",
"dir": "0",
"gps": "0",
"callsign": "WX2DX",
"email": "",
"phone": "",
"ham": "WX2DX",
"ham_show": "0",
"freq": "",
"note": "",
"im": "",
"twitter": null,
"web": "",
"unix": "1421874008",
"first": "William",
"last": "Smith",
"marker": "36181"
}
]
}
All I really need from it is the report_at, lat, lon, dir, first, last anyways. The rest I can do without. Is the above mentioned examples I tried a good or proper way to convert it? If not, then does anyone have a better suggestion than what I have been trying that I might be missing or overlooking which is a pretty good possibility due to be very green to this language? Thanks in advance!
EDIT: Since it has been brought to my attention I am not loading the JSON file this is what I have done to load it since the suggestions do not work as they apply to node.js and not a part of native javascript.
$.getJSON("./data/tracking.json", function(jsonData) {
var outGeoJson = {}
outGeoJson['properties'] = jsonData
outGeoJson['type']= "Feature"
outGeoJson['geometry']= {"type": "Point", "coordinates":
[jsonData['lat'], jsonData['lon']]}
console.log(outGeoJson)
});
This does load the file as it is displaying in the console as converted to GeoJSON. I will leave this as is for now unless there is a better solution.
If you do have jQuery in your project added, then you are almost there:
$.getJSON("./data/tracking.json", function(jsonData) {
/*
Here the anonymous function is called when the file has been downloaded.
Only then you can be sure that the JSON data is present and you can work with it's data.
You have to keep in mind if you are getting the file synchronously or asynchronously (default).
*/
});
var jsonData = "./data/tracking.json";
try replacing this with the next line.
var jsonData = require("./data/tracking.json");
or
import jsonData from "./data/tracking.json"; #es6 style
as #PatrickEvans mentioned you have to actually load the data rather than giving path as a string.

Find all parents elements in a Json file using jQuery

I'm currently working on a recursive menu which is built on top of jQuery which looks quite good already.
The structure of the JSon file containing the menu looks as the following:
[
{
"Id": "menuOfficeWebControlsForWebApplication",
"Title": "Office Web Controls",
"Resource": "/Documentation/Data/index.html" },
{
"Id": "menuGettingStarted",
"Title": "Getting Started",
"Resource": "/Documentation/Data/getting-started.html",
"Categories": [{
"Id": "menuCompilingFromSource",
"Title": "Compiling From Source",
"Resource": "/Documentation/Data/Getting-Started/compiling-from-source.html"
},{
"Id": "menuDownloadReleasePackage",
"Title": "Download Release Package",
"Resource": "/Documentation/Data/Getting-Started/downloading-release-package.html"
},{
"Id": "menuBuildingYourFirstApplication",
"Title": "Building your first application",
"Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html"
}]
}
]
Now, I can retrieve an item out of the menu using jQuery and the result might be this item:
{
"Id": "menuBuildingYourFirstApplication",
"Title": "Building your first application",
"Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html"
}
Now, I want to retrieve all the elements which are at a higher level and all the items which are directly below that item.
Any help is highly appreciated.
JQuery is for querying HTML elements from within the DOM of the current document, not for traversing objects or JSON expression strings.
In any case, given an object, there is no way to "discover" any objects, variables, or arrays that might hold a reference to it.
I'd recommend picking up a decent Javascript book and becoming familiar with the basics. Maybe stay away from things like JQuery at first as they can confuse things for you.

Retrieve the value from a JSONP formatted data object

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.
This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.
dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
{
"layerId": 5,
"layerName": "Building",
"value": "Name of item clicked",
"displayFieldName": "name",
"attributes": {
"ID": "123",
"name": "Name of item clicked",
"Variable1": "Some bit of information",
"Variable2": "Some other bit of information",
...
...
All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.
I've tried console.log(results[1].attributes.name); and console.log(results) with no success.
Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

Facebook Graph API - get page feed + full event info?

Is it possible to, in 1 request, get the feed of a page but with the full event info?
As it is now, if a shared event is posted, you only get back the link to that event, no picture or title:
{
"id": "xxx",
"from": {
"category": "Community",
"name": "xxx",
"id": "xxx"
},
"story": "xxx shared xxx's event.",
"link": "https://www.facebook.com/events/xxx/",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/xxx/posts/xxx"
},
{
"name": "Like",
"link": "https://www.facebook.com/xxx/posts/xxx"
}
],
"privacy": {
"value": ""
},
"type": "link",
"status_type": "shared_story",
"application": {
"name": "Links",
"id": "xxx"
},
"created_time": "2013-06-19T10:05:50+0000",
"updated_time": "2013-06-19T10:05:50+0000",
"likes": {
"data": [
{
"name": "xxx",
"id": "xxx"
}
],
"count": 1
}
}
If I understand correctly, you need to retrieve the events but you want to do it all at once with the feed because you want to retrieve the information on the feed anyway.
Before doing that, you must know that the feed doesn't contain all the events.... Once created, a link to the event is automatically shared on the page feed. It is only a reference, which can then be hidden. The event won't be displayed on the feed anymore even if it still exists.
Requesting two different objects at the same time
So, the feed doesn't have the events information and the events and posts (feed) are stored on 2 different tables. Therefore, you need to get the events independently from the feed:
The feed /PAGE_ID/feed
The events /PAGE_ID/events
And, as you wanted, Graph API allows you to do this in only one request:
/PAGE_ID?fields=feed,events
Additional fields
Note that either feed or events accept the limit and fields parameters. For example, events can be specified by:
events.limit(100).fields(location,name,owner,description,updated_time,venue)
Possible fields are given in the doc.
There is no way to get the "full info" at once. You will have to specify each field in the request. So, don't get the "full info", but just the information you really need.
There is a post from Facebook addressing this scenario using multi-queries and FQL (Facebook Query Language). This will allow you to make multiple FQL calls in one request.
https://developers.facebook.com/docs/technical-guides/fql/#multi

Categories

Resources