3

Basically I want to check and update a new column (in_downtown) in above table by using a function if the above data lies in following location of Downtown Seattle with these coordinates: Point A: ‐122.341518 47.578047 (longitude latitude) Point B: ‐122.284355 47.578047 (longitude latitude) Point C: ‐122.278004 47.630362 (longitude latitude) PointD: ‐122.371559 47.630362 (longitude latitude)

If one household (hhid) is found in downtown, set the value of the new column "in_downtown” to be 1; otherwise, leave it as null.

3
  • 1
    these four point make the square and you want to check that points in the table are within that square or not , am i right ? Jul 5, 2013 at 6:02
  • Screenshots are really hard for others to work with, please always show your data in a form that can be copied and pasted into sample tables for testing queries. Jul 8, 2013 at 1:36
  • why are there four points in your description but five points in the POLYGON example?
    – jimijazz
    Oct 29, 2015 at 1:07

3 Answers 3

2
BEGIN
FOR a IN (SELECT * FROM clean.households)
LOOP
UPDATE clean.households set geom=ST_GeomFromText('POINT(' || a.x_coord || ' ' || a.y_coord || ')',2285) where hhid ='a.hhid';
UPDATE clean.households set wkt = ST_AsText(ST_Transform(ST_GeomFromText('POINT(' || a.x_coord || ' ' || a.y_coord || ')',2285),4269))where hhid='a.hhid';
RETURN NEXT a;
END LOOP;

This is the answer that works. Thanks!

1
  • why? where are you setting your boundaries?
    – jimijazz
    Oct 29, 2015 at 1:08
0
BEGIN -- (IN wkt TEXT)
DECLARE pos TEXT;
DECLARE X FLOAT;
DECLARE Y FLOAT;
DECLARE spa INT;

DECLARE TOP_EDGE FLOAT;
DECLARE BOTTOM_EDGE FLOAT;
DECLARE LEFT_EDGE FLOAT;
DECLARE RIGHT_EDGE FLOAT;

-- You'll need to set the above variables here
TOP_EDGE = 47.630362;
BOTTOM_EDGE = 47.578047;
LEFT_EDGE = -122.341518;
RIGHT_EDGE = -122.371559;
-- End bounds setup

pos = LEFT(wkt, LENGTH(wkt) - 1);
pos = RIGHT(wkt, LENGTH(wkt) - 6);
spa = INSTR(pos, ' ');

X = FLOAT(LEFT(pos, spa));
Y = FLOAT(RIGHT(pos, LENGTH(pos) - spa - 1); -- May be off by one here.

RETURN CASE WHEN (Y < TOP_EDGE AND Y > BOTTOM_EDGE 
    AND X < RIGHT_EDGE AND X > LEFT_EDGE) THEN 1 ELSE NULL END;
END

This should be pretty close. It could be significantly simplified if you didn't store coordinates as a text.

0
0

you can use geometry object of polygon . you can do something like :

Declare @point geometry
Declare @polygon geometry
SET @point = geometry::STGeomFromText('POINT (-88.22 41.50000001)', 4326)
SET @polygon = geometry::STGeomFromText('POLYGON ((-88.2 41.5, -88.2 41.6, -88.3 41.6, -88.3 41.5, -88.2  41.5))', 4326)

Select case when @point.STIntersection(@polygon).ToString() = 'GEOMETRYCOLLECTION EMPTY' then 0 else 1 end
1
  • "Declare @point" ... what language is that? You seem to be using PostGIS functions, but what's with the DECLARE and SET stuff? Jul 8, 2013 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.