Check if given latitude and longitude is on the ploygon using in laravel controller - laravel-7

I have a tabel named Zone, there i have fields like id, zone_id,name,polygon. this data gets from traccar geofences. its stroes data correctly. now i need to find a given latitude and logitude in the range of polygon i got.
for example:
given points = $x = 26.421813284735137; $y = 43.9439818756710;
x is latitude and y is logitude.
polygon = POLYGON((26.421813284735137 43.94398187567106, 26.386908685914406 43.887332873404816, 26.153830842116705 43.957312477351465, 26.04340357033719 43.956808679085405, 26.01138700233561 43.98673788705223, 26.053647026517794 44.07652551095268, 26.262162148604205 44.08222631247017, 26.428194745096604 44.01096629350155, 26.421813284735137 43.94398187567106))
i am using laravel 7. i need to find it in the controller.

Related

Converting Miles to Degrees for postgis Sequelize

I am querying the PostGres database using POSTGIS extension as I want to get users in a radius.
I am following this post, where the following code is working fine:
Alert.findAll({
where: Sequelize.where(
Sequelize.fn('ST_DWithin',
Sequelize.col('position'),
Sequelize.fn('ST_SetSRID',
Sequelize.fn('ST_MakePoint',
req.query.long, req.query.lat),
4326),
0.032),
true)
})
.then(alerts => res.send(alerts))
.catch(next)
But I am encountering a strange problem, the radius is defined in degrees not miles, e.g. they said
Sample query to find all within 0.032 deg which is approximately 2 miles.
Then to customize the radius & validate my solution against above statement, I searched a lot and I wasn't able to find any formula to convert my miles into radius say I want to check in the radius of 10 miles. Then I checked this stack question, and run some calculations based on the formulas given in the accepted answer, using miles as 2 instead of 100, just to ensure if the approximation is correct but the answer was 0.42 not 0.32. e.g.
const latTraveledMiles = 2;
const latTraveledKM = 2 * 0.621371;
const latTraveledDeg = (1 / 110.54) * latTraveledKM;
const currentLat = 74.0064;
const longTraveledMiles = 2;
const longTraveledKM = 2 * 0.621371;
const longTraveledDeg = (1 / (111.320 * Math.cos(currentLat))) * longTraveledKM;
let degrees = Math.sqrt(Math.pow(latTraveledDeg, 2), Math.pow(longTraveledDeg, 2)); //should be 0.32
To make things easier, I would suggest you cast the geometry column position and ST_MakePoint to geography which will allow you to use metric input. You can read more about it here https://www.crunchydata.com/blog/postgis-and-the-geography-type
To add a geography column to your table you can use the following statement
-- first you add the column of type geography
ALTER TABLE your_table ADD COLUMN geog geography(point, 4326);
-- afterwards you populate it from your geometry column which is position
UPDATE your_table SET geog = position::geography;
You can now use ST_DWithin(geography gg1, geography gg2, double precision distance_meters); in your javascript logic, hence just substitute your radius value with 2*1.609344*1000 which translates to 2 miles times the factor to kilometers times 1000 to yield that value in meters.

Azure Data Factory Web API activity

We are using Azure data factory to get weather data from one of the API. We have to implement multiple activities first we need a table which holds all the latitude and longitude data then we need to build a Azure pipeline to loop through the locations ( coordinates ) and call the API to get the weather information.
Below are steps which I performed.
I am using look up activity to get the data from the table.
Then I am using Foreach activity to loop through the latitude and longitude.
I am using set variable activity where I am using one variable for latitude and one for longitude.
I am using web activity to pass the URL and GET response from the API.
I am able to achieve most of the output but the issue where I am stuck is the output URL is not fetching any data because the for some part of my URL the hyperlink which is blue color is removed and is not able to read.
Below are the screen grabs .
Two activities Lookup and foreach with four variables declared
My source dataset is a SQL table having latitude and longitude which i want.
This activity shows two set variable and web and copy activity.
I have added Name and Value of variable which will be passed onto the web URL
Below wen activity where I passed the weather API url
Dynamic content where I am passing the URL, key and latitude, longitude variable, format, no of days.
Final output where I am facing issue if you see the the hyperlink is removed for the URL after latitude value i.e. 41.4 so after that nothing is being read and data is not coming in JSON. How to fix this and how we can pass to variables in a URL because in my case the latitude and longitude is separated by a comma as a separator, and if I try to add comma it is not reading the URL
This is the final URL which I am trying to achieve through Azure Data Factory.
http://api.worldweatheronline.com/premium/v1/weather.ashx?key=*****************&q=41.14,80.68&format=JSON&num_of_days=5
What is the output of the query . Select lat ,long from Weather_location ? I think the long value is -80 in there and so you are having the issue . This is what I tried .
Created two variable Long and lat and set the value which you shared . Used the below expression .
#concat('http://api.worldweatheronline.com/premium/v1/weather.ashx?key=XXXXXX&q=',variables('lat')
,',',variables('long'),'&format=JSON&num_of_days=5')
It just works fine .
{
"data": {
"request": [
{
"type": "LatLon",
"query": "Lat 41.14 and Lon 80.68"
}
],
"current_condition": [
{
"observation_time": "11:3
.....
You are right when you said about the hyperlink , it does not show as complete ( see the screen shot below ) but it still works for me .
Input
{
"url": "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=XXXXXX=41.14,80.68&format=JSON&num_of_days=5",
"method": "GET",
"headers": {}
}
Edit on 1/15
As I undersand the intend is to use the API and copy the response JSON to a ADLE GEN 2 .
This is what I did when i tried .
Used the a copy activity select SINK as REST , base URL in the linked service was
http://api.worldweatheronline.com.
Create a new Variable : relativeurl :
#concat('/premium/v1/weather.ashx?key=yourkey&q=',variables('lat')
,',',variables('long'),'&format=JSON&num_of_days=5')
We will use this as a parameter in the SOURCE relative url .
GO to Copy Activity ( CA ) -> Source DS -> Open -> Parameter -> relativeurl
GO to Copy Activity ( CA ) -> Source -> you should see relativeurl ->#variables('relativeurl')
GO to Copy Activity ( CA ) -> Source DS -> Open ->Relative URL -#dataset().relativeurl
HTH

Once Shapefile is imported to Matlab, How to use longitude and latitude data from the created structure

I created a shapefie from a portal item in Arc GIS. So, on Matlab, I have this map, which shows the polygons outlined within NYC:
MapNYC
The shapefile I have is called "Sewershed.shp".
In Matlab, I type in:
shaperead('Sewershed');
Then, I get a 17x1 structure
17x1 Structure
So, the heading, "Bounding Box" are the longitude and latitude boundaries that define the Sewersheds (under heading "SewerShed")
The polygons in the map I showed above are the outlines of the sewer shed.
Now, I have an Excel spreadsheet that lists items as defined by longitude latitudes. I need to identify which Sewer Shed each item belongs to.
Excel Spreadsheet Image
I'd appreciate any help with this!
Josephina
assuming your boundingbox record has [x0,y0, x1,y1] where x0-x1 is the range of the longitude and y0-y1 is the range of your latitude. If you have a new coordinate newpt=[long1, lat1], you just need to compare long1 with x0-x1 and lat1 with y0-y1 and find the records that they belong to.
here is my sample test code
mydata(1).BoundingBox=[1,2,5,7]
mydata(2).BoundingBox=[2,3,6,9];
mydata(3).BoundingBox=[7,1,8,20];
bbx=cell2mat({mydata(:).BoundingBox}')
newpt=[4,3]
find(newpt(1)>=bbx(:,1) & newpt(1)<=bbx(:,3) & newpt(2)>=bbx(:,2) & newpt(2)<=bbx(:,4))

How can I retrieve the geometry coordinates of a NpgsqlTypes.PostgisGeometry type field from the NpgsqlDataReader?

.NET 4.5, C#, Npgsql 3.1.0
I have a query which retrieves a Postgis geometry field - the only way I could see of doing this was:
public class pgRasterChart
{
...
public NpgsqlTypes.PostgisGeometry GEOMETRY;
...
}
...
NpgsqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
pgRasterChart chart = new pgRasterChart();
chart.GEOMETRY = (PostgisGeometry) reader.GetValue(21);
...
This functions but I need to get at the coordinates of the GEOMETRY field and I can't find a way of doing that? I want to use the coordinates to display the results on an OpenLayers map.
Any answers most gratefully received. This is my first post so my apologies if the etiquette is clumsy or question unclear.
Providing another answer because the the link above to the documentation for PostGisTypes is now broken.
PostGisGeometry is an abstract base class that does not contain anything more exiting than the SRID. Instead, you want to cast the object obtained by your datareader to the appropriate type (any of the following):
PostGisLineString
PostGisMultiLineString
PostGisMultiPoint
PostGisMultiPolygon
PostGisPoint
PostGisPolygon
These classes have ways of getting to the coordinates.
eg:
...
NpgsqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
var geom = (PostgisLineString) reader.GetValue(0);
var firstCoordinate = geom[0]; // Coordinate in linestring at index 0
var X = firstCoordinate.X;
var Y = firstCoordinate.Y;
...
As you can see here
https://github.com/npgsql/npgsql/blob/dev/src/Npgsql.LegacyPostgis/PostgisTypes.cs
PostgisGeometry types are a set of xy pairs.
For example, a linestring is an array of points, a polygon is an array of rings and so on..
You could traverse those structures and get the coordinates.
However, if you just want to display geometries using openlayers, I suggest you to use the wkt format.
You should change your query, selecting st_astext(geometry) instead of geometry, than treat the result as a string and give it back to OpenLayers.
Then use OpenLayers.Geometry.fromWKT to parse the WKT into an OpenLayers.Geometry

Get objects with max value from grouped by linq query

I got the following linq query:
var invadersOrderedInColumns = from i in invaders
group i by i.GetPosition().X;
This will order the invaders with the same X position. The next thing I want to do is retrieve the invader with the highest Y value from each of those columns.
Imagine if you will each invader as a black blok in the following image. This will represent the invaders after the above linq query. Each X = Value is the key.
Now, from each of these groups (columns), I want to get the invaders with the highest Y position (so the bottom invader of each column when you look at the picture):
How can I get this done with a Linq query?
I don't much care for the query syntax, but in extension method syntax it would look something like this.
var invadersOrderedInColumns = invaders
.GroupBy(d => d.GetPosition().X)
.Select(d => d.OrderByDescending(y => y.GetPosition().Y).First());

Resources