PostGIS - Finding aggregated counts on dissolved polygons - count

I have set this WITH query to get dissolved polygons where polygons overlap:
CREATE TABLE public.dissolve_intrsct AS
( WITH dissolved AS(
SELECT ST_Union(pt.geom) as geom, count(geom) as buff_ct,
count(pt.count) as intrsct_ct
FROM public.intrsct_buff as pt
)
SELECT (ST_DUMP(geom)).geom::geometry(POLYGON, 2263), intrsct_ct, buff_ct
FROM dissolved);
My output returns the geometry(good), but also returns the total count of all geometries before the query(59144) for both intrsct_buff and buff_ct
I want the query to return the number of polygons that were dissolved to create the new polygon. (i.e. If three polygons overlap, after this query I want buff_ct to return 3, along with intrsct_ct just returning the original count I had in public.intrsct_buff)

I found what needed to be done..
After creating this table and adding a PRIMARY KEY ID, I then used ST_Intersects to find where my dissolved polygons intersected with the original buffer, and counted the number of intersecting polygons.
See code below:
`
CREATE TABLE dissolve_ct AS(
SELECT ds.id, ds.geom, count(pt.geom) AS
buff_ct
FROM public.dissolve_intrsct as ds
LEFT JOIN public.intrsct_buff as pt
ON ST_Intersects(ds.geom, pt.geom)
GROUP BY ds.id
);
`

Related

Create Edge list for (900+) nodes (in R)

I have a problem creating an edge list of 406351 edges (possible combinations of two nodes out of 902 unique nodes). The nodes correspond to doctors, and the edges correspond to the number of patients two nodes share in a period of time (one year).
I have medical claims data, with one observation being one visit of one patient to a particular doctor.
So far, what worked for me was to: First, table the doctors against the patient's id, obtaining the number of visits each patient had with each doctor. After that, I took the list of unique doctors' id, and create an edge list with all the possible 2 elements combinations. Finally, I "filled up" the edge list with a for loop that looks into each combination of columns(doctors) in the table, and counts how many particular patients had visits>0 for both columns(doctors).
This works, but my problem is that the loop is too slow and I would like to know if there is a faster way of doing this.
Here an example of my approach so far:
#DATA
case_number<-c("123","3456","5433","5678","9874","8566")
doctor_id<-c("333","444","555","333","666","555")
patient_id<-c("1","2","2","2","1","1")
DATA<-data.frame(case_number,doctor_id,patient_id)
#Table doc vs patients
table<- as.data.table(as.data.frame.matrix(table(DATA$patient_id,
DATA$doctor_id)))
#Create edge list
Drs<-unique(DATA$doctor_id)
edge_list<-as.data.table(t(combn(as.vector(unique(Drs)), 2)))
#'fill up' edge list
for (z in colnames(table)) {
for (y in colnames(table)) {
edge_list<-edge_list[(V1==z & V2==y) | (V2==z & V1==y),
Weight:=nrow(table[table[[z]]>0 & table[[y]]>0])]}}
Never mind. I realized that with some matrix algebra I could create an adjacency matrix and then use the network package to create the edge list. Comment if you need the code!

Calculate length of a shared edge of two polygones in PostGIS

I have a database of several polygones. Each polygon has a lot of nodes which define it. Additionally each polygone has at least one neighbour. For a calculation I need to determine the length of the shared edge of two neighboured polygones. In the example picture such a case is shown. The two polygones share the red edges. How can I calculate the length of the red edge with the help of PostGIS? I didn't find a function for that.
You can get the intersection of two polygons, then get the length(s) of any LineStrings. For example, take two geometries:
SELECT ST_Length(ST_CollectionExtract(ST_Intersection(a_geom, b_geom), 2))
FROM (
SELECT
'POLYGON((70 170,160 250,240 220,270 130,185 62,90 80,70 170))'::geometry AS a_geom,
'POLYGON((160 250,236 314,380 290,390 150,270 130,240 220,160 250))'::geometry AS b_geom
) f;
Or do this for all pairs of polygons in a table mypoly that touch:
SELECT a.gid AS gid_a, b.gid AS gid_b,
ST_Length(ST_CollectionExtract(ST_Intersection(a.geom, b.geom), 2))
FROM mypoly a, mypoly b
WHERE a.gid < b.gid AND ST_Touches(a.geom, b.geom);
You can also explore the Topology extension, which is part of PostGIS 2.x.

How to UPDATE multiple columns using a correlated subquery in SQLite?

I want to update multiple columns in a table using a correlated subquery. Updating a single column is straightforward:
UPDATE route
SET temperature = (SELECT amb_temp.temperature
FROM amb_temp.temperature
WHERE amb_temp.location = route.location)
However, I'd like to update several columns of the route table. As the subquery is much more complex in reality (JOIN with a nested subquery using SpatiaLite functions), I want to avoid repeating it like this:
UPDATE route
SET
temperature = (SELECT amb_temp.temperature
FROM amb_temp.temperature
WHERE amb_temp.location = route.location),
error = (SELECT amb_temp.error
FROM amb_temp.temperature
WHERE amb_temp.location = route.location),
Ideally, SQLite would let me do something like this:
UPDATE route
SET (temperature, error) = (SELECT amb_temp.temperature, amb_temp.error
FROM amb_temp.temperature
WHERE amb_temp.location = route.location)
Alas, that is not possible. Can this be solved in another way?
Here's what I've been considering so far:
use INSERT OR REPLACE as proposed in this answer. It seems it's not possible to refer to the route table in the subquery.
prepend the UPDATE query with a WITH clause, but I don't think that is useful in this case.
For completeness sake, here's the actual SQL query I'm working on:
UPDATE route SET (temperature, time_distance) = ( -- (C)
SELECT -- (B)
temperature.Temp,
MIN(ABS(julianday(temperature.Date_HrMn)
- julianday(route.date_time))) AS datetime_dist
FROM temperature
JOIN (
SELECT -- (A)
*, Distance(stations.geometry,route.geometry) AS distance
FROM stations
WHERE EXISTS (
SELECT 1
FROM temperature
WHERE stations.USAF = temperature.USAF
AND stations.WBAN_ID = temperature.NCDC
LIMIT 1
)
GROUP BY stations.geometry
ORDER BY distance
LIMIT 1
) tmp
ON tmp.USAF = temperature.USAF
AND tmp.WBAN_ID = temperature.NCDC
)
High-level description of this query:
using geometry (= longitude & latitude) and date_time from the route table,
(A) find the weather station (stations table, uniquely identified by the USAF and NCDC/WBAN_ID columns)
closest to the given longitude/latitude (geometry)
for which temperatures are present in the temperature table
(B) find the temperature table row
for the weather station found above
closest in time to the given timestamp
(C) store the temperature and "time_distance" in the route table

Retrieving latitude & longitude values from database and mapping them using Google Map API in asp

I have two fields Hospital Name & City. When I click on the submit button a new page will be shown displaying the location of the specified hospital. How can I achieve this task?
A location table already exists which contains hospital name,longitude & latitude values. On the basis of inputted hospital name, I want to retrieve its respective latitude & longitude values and map them on the next page displaying a google map.
See Here : -> https://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/346407/getting-latitude-and-longitude-info-into-text-boxes>
Find your Lat Long coordinates on Click and open it in new window.
Assuming your database is either MS SQL or MySQL
The following SQL query uses Spherical Law of Cosines to calculate the distance between a coordinate and coordinates in a table. It limits result to 10 and orders by distance.
d = acos( sin(lat1).sin(lat2) + cos(lat1).cos(lat2).cos(lng2-lng1) ).R
Were R = earth’s radius (mean radius = 3,959 miles or 6,371km)
SELECT hospital name,latitude,longitude, (3959 * acos(cos(radians(center_latitude))
* cos(radians(latitude)) * cos(radians(longitude)
- radians(center_longitude)) + sin(radians($center_latitude))
* sin( radians( latitude )))) AS distance FROM table
ORDER BY distance LIMIT 0 , 10
Where center_latitude & center_longitude are coordinates of location.

SQLite: Query for Latitude/Longitude Delta

I have a SQLite table with fields Latitude and Longitude (neg and pos numbers)
I have a latitude *delta* and a longitude *delta* of (for example '23') based on a Latitude of 50 and a longitude of 75 (again, for example)
How do I query the table for a list of all records which are + or - 23 latitudes and + or - 23 longitudes when there are both positive and negative latitude and longitude values in the table.
The lat/long fields are of type NUMERIC and are indexed ASC.
select [fields]
from [table]
where latitude of 50 -+23 and
longitude of 75 -+23
Lookup the delta and stating values first
Calculate the +/- values based on the delta and the starting values per-record
Use the "BETWEEN operator" to get the result (scroll down on that link to find the info)
If you're using an ORM between you and the database, or a programming language on top of the DB, you might be able to do steps 1 and 2 on the fly, thereby only executing one DB query in step 3.

Resources