Find nearest line from linestring PostGIS - postgis

I want to find the closet line to a given point using PostGIS on postgres db. But I can't extract lines from LineString data structure using PostGIS's functions.
This is one of my LineString example:
LINESTRING(51.4108421 35.7576013,51.4103935 35.7577687,51.41033 35.7578188)
How can I resolve this problem?

Related

geom_sf: plot multiple series

I have a sf polygon dataframe with multiple
series (T1, T2, T3, all on the same scale:
they're observations at different time points).
I can plot say T1 with
ggplot(map)+geom_sf(aes(fill=T1))
what I'd like to do is plot all three (T1, T2 and T3)
as facets (separate maps) on the same drawing.
I'm sure there's a way to do this, but I can't find it.
Can anyone tell me how? Thanks!
ADDED: Two additional notes on this question.
First, the data structure described above is
one that could be plotted using spplot, with
the T's being the arguments to spplot's zcol argument.
So in this connection, my question amounts to
asking how to convert an spplot structure to
be usable by geom_sf.
Second, suppose I use sf to read in a shp file
for say 20 polygons. I also have a data frame
consisting of stacked observations for these
same polygons, say for 3 periods, so the
dataframe has 60 rows. How do I merge these
in order to be usable? Can I just stack
3 copies of the sf structure, and than cbind the
dataframe (assuming the rows match up correctly)?
At least in one sense this turns out to be very simple. Given a data structure (ds_sp) that can be plotted with spplot, you can just do the following:
ds_sf <- st_as_sf(ds_sp) # convert to sf form
plot(ds_sf[c("T1","T2")]) # plot the desired series
This isn't quite the same as using facet_wrap with ggplot, but at least it gives you something to work with.
ANOTHER LATER ADDITION : As to the longitudinal + facet_wrap issue, the following seems to work:
If necessary, create a data frame (df1) with the
longitudinal data (longit), an area indicator (fips) and a time
indicator (date) which will be used for faceting, and
anything else you may need.
If necessary, create an sf-compatible version of
the spatial geometry via st_to_sf, as new_poly .
This will be of classes "sf" and "data.frame"
and should have a spatial indicator matching fips
in df1.
Merge the two:
data_new<-df1<-dplyr::inner_join(df1,new_poly,by="fips",all.x=TRUE)
Now produce the plot
ggplot(data_new)+geom_sf(aes(fill=longit,geometry=geometry))+facet_wrap(~date)
and make adjustments from there.

In Snowflake, can you create a circle polygon from a given centre point and radius?

I have a table containing geometrics points (called [Polygon_Centre]) and distances (called [Desired_Radius]). Anybody know which Snowflake function I could use to create polygon circles from this?
I'm trying to achieve a similar result to the Trade Area tool in Alteryx if anybody is familiar with that.
I was hoping for something like the following (which doesn't work):
ST_MAKEPOLYGON('Circle', [Polygon_Centre], [Desired_Radius])
Does anybody know how to create a circle polygon from a given centre point and radius in Snowflake?
Thanks everybody. I've found and used the following solution:
Calculate 120 points around the circle mathematically
Turn this into an ordered list with ST_COLLECT() (the order is critical and the start and end must match)
Use ST_MAKELINE() to turn these points into a line
Create the circle with ST_MAKEPOLYGON().

Calculate the length of polylines in polygon (TSQL, Geometry)

I have a lot of polylines (polyline) and a polygon. Polylines can intersect with the polygon in different ways:
no intersection (polyline out of polygon)
no intersetion (polyline in a polygon)
one intersection (polyline starts/ends in polygon)
multiple intersections (polyline can for example start in polygon, go out of it and then return back inside...)
I need to calculate the total length of all polylines within the polygon. I'm using MSSQL 2016 Express version. Can it be done with just TSQL?
The answer is Yes - TSQL is turing-complete, meaning that you can implement any algorithm you want with it. It is more convenient to use than a turing machine but (arguably) less so than, for example, C# with a fully fledged development environment.

Redis GREORADIUS: include altitude?

I am looking to use Redis' GEORADIUS command.
However, this command only searches within the range of a two-dimensional circle around a given longitude and latitude. I need to also search with an altitude, thus finding results within the range of a three-dimensional sphere.
How would I achieve this in Redis?
I believe what you're actually trying to do is search within a 3D cylinder (or some kind of a cone if you take into account the Earth's spherical nature).
An altitude search isn't natively included with Redis' geospatial indices, but you could store that property in its own Sorted Set as score. Then, you can perform a range search (ZRANGEBYSCORE) on the altitude and intersect (ZINTER) the results with those from the radius query (hint: use a temporary key to STORE results).
For your reference, this is the approach I took with the xyzsets in geo.lua.

PostGIS: ST_Covers fails for extremum point location

I am using PostGis to save geographical data and then use ST_Covers() function to find if a point or location lies with in a geography or not.
I can save a location with extremum values like:
-90,180;90,180;90,-180;-90,-180;-90,180
These values will tend to cover whole globe (lies along side the poles). I can save such a location and can retrieve it with no worries. However, when I call ST_Covers() on such locations, to find if a location lies within such location, the function fails and gives parse error.
I am using Postgis 1.5.3, upgraded to 2.0.2 but with little luck.
Can anybody please help me if something like this is possible. and how can I call ST_Covers() on such a location without any error.
PostGIS geography will not correctly interpret any edge greater than or equal to 180 degrees. Re-formulate your box with shorter edges and you should get what you desire.

Resources