How to calculate width and lengths of street in PostgreSQL? - postgresql

I am already aware that this question, more specifically, should have been directed to GIS StackExchange. I already asked the question here but sadly, got no answers or any suggestions. I used ST_ClosestPoint() to project a point to the nearest line (point on line interpolation) using this approach.
Select ST_ClosestPoint(line, pt) As closest_pt_line
From
(Select ad.geom As pt,
st.geom As line,
ST_Distance(ad.geom, st.geom) As d
From ad, st
Where ST_DWithin(st.geom, ad.geom, 10.0)
Order By d
) As foo;
Based on this projected point, I need to calculate the length of street in both directions and width of street by making using of building polygons layer. The scenario can be visualized like this:
Visualization of the scenario
I am aware of ST_Length function but it returns the length of whole linestring rather than length 1 and length 2. Any advice to calculate lengths of street and width would be highly appreciated?

Related

How to filter points in postgres?

I have a table with points in the Postgres. The points are from the flight path. I need to filter out some points.
.
My question would be how can I select only points which are in line and then make a line from the selected points only if parallel lines are in no more then 20m distance in between lines. Turning points should be ignored.
What I have done so far is to select points that are in one line
WITH routes as (
SELECT
geom,
heading-lag(heading) over (order by time) AS direction
FROM mytable
)
SELECT direction, geom
FROM routes WHERE direction between -10 AND 10;
In my query, I calculated direction from heading, and selected points with a minor difference in the heading.
However, I don't know how to continue.
EDIT
Link to fiddler data table http://sqlfiddle.com/#!17/3262c/9/0
With my query from above, I can filter the points which are marked in red lines. How can I add those points in variables like line1=...
line2=... line3=... line4=... and line5=... ?
Thank you for any help.

In Tableau Map plot multiple sites at same lat long

I have a data where at one latitude and longitude multiple shops are located.
For Example.
Latitude Longitude ShopId Type
6.24458 50.001756 101 Saloon
6.24458 50.001756 102 Groceory
6.24458 50.001756 103 Pharmacy
6.24458 50.001756 104 FishMarket
When on map I am plotting using above latitude & longitude I am getting single mark. And when I hover the mark I am getting single shop details but I want 4 marks and on each mark it should show respective shopid and Type.
I am new to Tableau and not able to figure out how to do it.
You are likely getting 4 marks displayed at the same location. So when you click on the mark you see, then you are only selecting the top mark. You can verify this by dragging over the mark to select all the marks within a selection rectangle. If you then, right click and view data, you should see all 4 marks.
Another thing that can help when you have overlapping marks, is to make the marks partially transparent and add a border around the marks. Both options are available by clicking on the Color button on the marks card to get to the advanced color settings.
If this is not the behavior you want, you have a couple of options. One easy approach is to add a little random noise to each latitude and longitude (called jitter). Adding a little jitter makes the marks visible, although the size of the jittering depends on your data and scale. Jittering is especially useful if all your points are geocoded to the same situation - say if every building with a Los Angeles address is treated as if it is located at city hall. In that case, the geocoding distorts the data to a degree that jittering is just fine.
The undocumented RANDOM() function is an easy way to add some jitter. Excel and Hyper Extracts support RANDOM() among other data source types. It returns a number between 0 and 1.
The other options involve treating your coordinates as continuous dimensions instead of measures, and then using some other visual attribute size, color etc to indicate the number of items at each location. It is often useful to combine nearby items with some sort of grid or hex bin function -- In this case, instead of adding random noise to each coordinate, you round or truncate it in someway to effectively snap points to a grid. The ROUND() and HEXBINX() HEXBINY() functions are useful here. When using this approach, be sure your packed coordinate fields are continuous dimensions and have the appropriate Latitude or Longitude geographic role.
Finally, take a look at the density mark type. It can make visual heat maps, either working with exact data points or grid packed points.

Convert st_astext result to single text columns

I need some help with postgis.
I have made a point geometry that have converted to text through st_astext.
I currently have these three columns:
object_id(uuid) komnr(int) st_astext_result(text)
2bcc1b26-0a7c-472a-9e74-9cadfefc2d44 320 POINT(687989.85 6135746.91)
I would like two columns each containing the east and north coordinate in the same syntax as it is now. Do you have any suggestions to how i may do this?
Preferred outcome would be:
east north
687989.85 6135746.91

Put points on the map, longitude/latitude not where they should be although SRID the same

Thank you for looking at this - I'm sure it's a simple question but this is all very new to me and after a lot of internet searching my points are still in the sea!
I have a set of UK postcode data that I have converted into latitude and longitude. I also have a UK counties shape file.
The code that I have used to convert the latitude/longitude to a geometry point column is as follows:
ALTER TABLE colic ADD COLUMN longlat geometry(POINT,27700);
update colic set longlat = st_setsrid(st_point( longitude, latitude), 27700);
And this produces a column that has values as follows (I've only included a couple!):
"0101000020346C000048BF7D1D3867E83FC05B2041F1334A40"
"0101000020346C0000F085C954C1A8F7BFA1F831E6AE954A40"
"0101000020346C000020D26F5F07CEF4BFE3361AC05B504A40"
"0101000020346C00007D3F355EBA49F73FB6847CD0B3614A40"
The .prj from my shape file is:
PROJCS["British_National_Grid", GEOGCS["GCS_OSGB_1936",
DATUM["D_OSGB_1936", SPHEROID["Airy_1830",6377563.396,299.3249646]],
PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",400000.0],
PARAMETER["False_Northing",-100000.0],
PARAMETER["Central_Meridian",-2.0],
PARAMETER["Scale_Factor",0.9996012717],
PARAMETER["Latitude_Of_Origin",49.0], UNIT["Meter",1.0]]
And I set its SRID to 27700 when I upload it using the PostGIS shapefile uploader.
In PG Admin, when I ask:
select st_srid(geom) from counties limit 1;
select st_srid(longlat) from colic limit 1;
I get 27700 for both.
But...when I try any spatial query I get no response, and when I load the layers into quantum, the point data are off the tip of Cornwall in the north Atlantic, and only represented as one dot on the scale of the counties map.If I zoom in on them, they are 'there', but just not in east anglia where they should be!
This is my first attempt at both SQL and mapping - I'm sure there is something really simple that I have missed.
You are mixing up spatial references (SRS). The SRS in the .prj file (aka SRID=27700) is projected eastings and northings, which have units of metres. This is not latitude and longitude! Furthermore, it is a deception to call a column longlat when it isn't for longitude/latitude coordinates.
If you try to insert latitude/longitude in a column with SRID=27700, the points will not behave as expected, e.g., they will be in the far bottom left corner of a map.
If you have lat/long data from WGS84 (SRID=4326), you can transform this to eastings and northings:
UPDATE colic SET
geom = ST_Transform(ST_SetSRID(ST_Point(longitude, latitude), 4326), 27700);

crop a DEM and keep proper referencing

I've downloaded the GMTED2010 digital elevation model (DEM) from the USGS, tiles N30W120_150 and N30W150_150. It's a 500 m DEM. I would like to join these into on file in matlab so I tried:
Z=[Z150 Z120]; %Join the dems at their matching side
R=R120; %Create a new spatial referencing object based on the old one.
R.Lonlim=[R150.Lonlim(1),R120.Lonlim(2)]; %Modify what changed in R
R.RasterSize=size(Z); %Adjust the RasterSize referenced in R.
This works, but about 1/3 is pacific ocean which I'd like to remove for size/computation sake. Importantly, though, would be to keep the referencing information correct in R.
%Crop DEM to remove most ocean pixels. about a 1/3 of dem.
Z=Z(:,floor(20/0.0042):end); %crop Z from -90 to -130 (20 degrees from the start (-150) longitude at 0.0042 deg/pixel according to the referencing info
R.Lonlim=[-150.0001+floor(20/0.0042)*0.0042, -90.0001]; %substract number of pixels cropped times deg/pixel value
R.RasterSize=size(Z); %Adjust the RasterSize referenced in R.
The problem is when I try to look at the elevation at a specific location such as:
lat=[39 38 47.22];
lat=dms2degrees(lat);
long=[-105 52 9.84];
long=dms2degrees(long);
hght=ltln2val(Z,R,lat,long);
the value given by hght isn't correct. In fact, if I look at a subset around the lat,long:
[r c]=latlon2pix(R,lat,long);
Z(r-30:r+3,c-30:c+30)
The value in hght isn't even in this centered subset. I'd understand if I was 1 pixel off or so, but why am I so far off? Using hght=ltln2val(Z,R,lat,long) WITHOUT cropping but still combined gives the correct (same value as with just the Z120 DEM) value so I assume my referencing object is wrong.
Any help someone might provide is appreciated.
Thanks
Dominik