Polygon another location - geolocation

Looking google maps have located 4 points and have created a polygon with them in this way
insert into dummy_zip values (4,'prueba_directa','2016-04-13
08:08:15.973731','prueba_directa',1,1,100,'2016-04-13
08:08:15.973731',(SELECT
ST_Transform(ST_GeomFromText('MULTIPOLYGON(((37.986504
-1.121951,37.986741 -1.121243,37.985794 -1.120792,37.985608 -1.121479,37.986504 -1.121951)))',4326),900913)));
but when displayed on the map they are in a completely different location than where they should be?

Swap the axis order, which should be (X Y) or (long lat).
You can use ST_FlipCoordinates to fix any existing data.

Related

How do I return coordinates of nodes in neo4j

I am looking at visualising neo4j graph data in Tableau but Tableau requires the coordinates to be included in the data before it can plot the nodes in a graph.
By coordinates I just mean an x & y value so that I can plot nodes and edges on a 2d graph. There aren't any geographic/address requirements
Is there any way to return coordinates in a result set using cypher? Something along the lines of
MATCH (j:Job)-[:PARENT_OF]->(w:Workflow)
RETURN j, coords(j), w, coords(w)
There is nothing obvious that I can see having searched the web and also stackoverflow. I have searched the APOC procedures with terms such as "spatial", "coordinates" and "position" but so far I've had no joy.
I'm using neo4j community 3.3.1
EDIT: Looking at howto graph visualization step by step this suggests that you need to use external libraries in order to do this rather than getting the information from neo4j. I appreciate the visualisations are fluid and can be moved about but I would have thought that given the neo4j server has crunched the geospatial algorithms already, it would be something that could somehow be output via a function or procedure.
APOC does have a couple of procedures for getting the coordinates of an address: apoc.spatial.geocodeOnce and apoc.spatial.geocode.
For example, this query:
CALL apoc.spatial.geocodeOnce('21 rue Paul Bellamy 44000 NANTES FRANCE') YIELD location
RETURN location;
produces this result:
{
"description": "21, Rue Paul Bellamy, Talensac - Pont Morand, Hauts-Pavés - Saint-Félix, Nantes, Loire-Atlantique, Pays de la Loire, France métropolitaine, 44000, France",
"latitude": 47.2221667,
"longitude": -1.5566625
}
Modifying the example in your question (assuming that j and w have address properties), this should work:
MATCH (j:Job)-[:PARENT_OF]->(w:Workflow)
CALL apoc.spatial.geocodeOnce(j.address) YIELD location AS jLoc
CALL apoc.spatial.geocodeOnce(w.address) YIELD location AS wLoc
RETURN j, jLoc, w, wLoc;
When I asked this question I didn't understand that what actually happens is a frontend algorithm sorts and orders the nodes dynamically and therefore no coordinates are provided.

Combine feature collection into polygon turfjs

I'm trying to find out if a point lies within my country boundary. I'm using this data set as my country boundary: http://eric.clst.org/wupl/Stuff/gz_2010_us_outline_5m.json
And currently I'm check this point [-97.5164,35.4676]
function(){
var pt = turf.point(origin);
return turf.inside(pt, gz_2010_us_outline_5m);
}
How do I setup my geojson boundary data with turfjs so that it can be combined into a polygon to check if the point falls inside?
Just use turfs http://turfjs.org/docs/#lineToPolygon which Converts (Multi)LineString(s) to Polygon(s).

Mapbox GL JS - Analysis of Filtered Markers

I'm new to Mapbox GL JS, and am loving it! A couple of things I've run into when filtering markers on a GeoJson source that I'm wondering if someone can help me out with...here's a sample of my filter logic:
function applyFilters() {
var filters = ["all", ["==", "$type", "Point"]];
if (document.getElementById('filter1checkbox').checked)
filters.push(['==', 'property1', true]);
if (document.getElementById('filter2checkbox').checked)
filters.push(['==', 'property2', true]);
map.setFilter('markers', filters);
var llb = new mapboxgl.LngLatBounds(geojsonExtent(markers));
map.fitBounds(llb);
map.setCenter(llb.getCenter());
}
And here's my questions:
Once my filter is applied, is there a way to get a count of markers that matched the filter (Your search returned {X} items)?
When I use geojsonExtent to get the bounds of the marker collection, it doesn't seem to be taking the filter into account. Is there a way to get at the data behind the filter to pass into geojsonExtent?
Any advice on where to go for these items?
Once my filter is applied, is there a way to get a count of markers that matched the filter (Your search returned {X} items)?
You can get the number of filtered markers currently visible in the viewport by running
map.queryRenderedFeatures({layers: ['markers']).length
There is no way to get the total number of filtered markers across the whole map.
When I use geojsonExtent to get the bounds of the marker collection, it doesn't seem to be taking the filter into account. Is there a way to get at the data behind the filter to pass into geojsonExtent?
You can use queryRenderedFeatures for this too! (Note: this code is untested)
geojsonExtent({
type: 'FeatureCollection',
features: map.queryRenderedFeatures({layers: ['markers']).length
});

finding locations within a particular distance using db2

I am using html5 geolocation api to get my position in latitude and longitude. I want to store them in a table of locations and want to retrieve those locations within a particular distance.
my current latitudes and longitudes are stored in variables "latval", "longval", "distance"
My table is "location"
columns are "location", "lat", "long"
I am using DB2 Express C as database and latitude and longitude columns are set as double type now. What type should I use to store these values and what would be the query to get location names within a distance
Thank you.
It looks like there's an extension for Express C that includes Spatial processing. I've never used it (and can't seem to get access at the moment), so I can't speak to it. I'm assuming that you'd want to use that (find all locations within a radius is a pretty standard query).
If for some reason you can't use the extension, here's what I would do:
Keep your table as-is, or maybe use a float data-type, although please use full attribute names (there's no reason to truncate them). For simple needs, the name of the 'location' can be stored in the table, but you may want to give it a numeric id if more than one thing is at the same location (so actual points are only in there once).
You're also going to want indicies covering latitude and longitude (probably one each way, or one covering each column).
Then, given a starting position and distance, use this query:
SELECT name, latitude, longitude
FROM location
WHERE (latitude >= :currentLatitude - :distance
AND latitude <= :currentLatitude + :distance)
AND (longitude >= :currentLongitude - :distance
AND longitude <= :currentLongitude + :distance)
-- The previous four lines reduce the points selected to a box.
-- This is, or course, not completely correct, but should allow
-- the query to use the indicies to greatly reduce the initial
-- set of points evaluated.
-- You may wish to flip the condition and use ABS(), but
-- I don't think that would use the index...
AND POWER(latitude - :currentLatitude, 2) + POWER(longitude - :currentLongitude, 2)
<= POWER (:distance, 2)
-- This uses the pythagorean theorem to find all points within the specified
-- distance. This works best if the points have been pre-limited in some
-- way, because an index would almost certainly not be used otherwise.
-- Note that, on a spherical surface, this isn't completely accurate
-- - namely, distances between longitude points get shorter the farther
-- from the equator the latitude is -
-- but for your purposes is likely to be fine.
EDIT:
Found this after searching for 2 seconds on google, which also reminded me that :distance would be in the wrong units. The revised query is:
WITH Nearby (name, latitude, longitude, dist) as (
SELECT name, latitdude, longitude,
ACOS(SIN(RADIANS(latitude)) * SIN(RADIANS(:currentLatitude)) +
COS(RADIANS(latitude)) * COS(RADIANS(:currentLatitude)) *
COS(RADIANS(:currentLongitude - longitude))) * :RADIUS_OF_EARTH as dist
FROM location
WHERE (latitude >= :currentLatitude - DEGREES(:distance / :RADIUS_OF_EARTH)
AND latitude <= :currentLatitude + DEGREES(:distance / :RADIUS_OF_EARTH))
AND (longitude >= :currentLongitude -
DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude)))
AND longitude <= :currentLongitude +
DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude))))
)
SELECT *
FROM Nearby
WHERE dist <= :distance
Please note that wrapping the distance function in a UDF marked DETERMINISTIC would allow it to be placed in both the SELECT and HAVING portions, but only actually be called once, eliminating the need for the CTE.

searching for closest number in a rails app

Give two parameters which correspond to two attributes on an object how can one find 20 records in a database that are closest to those two numbers.
The parameters you have are x, and y. The object also has those attributes. For example. x = 1, and y = 9999. You need to find the record that is the closest to x and y.
That depends on how you define the distance between two points. If you are using a two-dimensional cartesian coordinate system, this SQL statement will work:
SELECT id, x, y FROM points ORDER BY SQRT(POWER((X-x),2)+POWER((Y-y),2)) ASC LIMIT 20;
Where X,Y are the inputs.
It sounds like you're using geolocated data. If your database backend is Postgres, check to see if you have or can install the PostGIS extensions. This gives you very fast tools which give you searches like 'search for the nearest thing to this point', 'search for everything within this circle', 'search for everything within this square', and so on.
http://postgis.refractions.net/
You would do something like this:
CREATE INDEX [indexname] ON [tablename] USING GIST ( [geometrycolumn] gist_geometry_ops);
Then you can do something like this - find everything within 100 metres of a point:
SELECT * FROM GEOTABLE WHERE
GEOM && GeometryFromText(’BOX3D(900 900,1100 1100)’,-1) AND
Distance(GeometryFromText(’POINT(1000 1000)’,-1),GEOM) < 100;
Examples from the manual.

Resources