[Qgis-user] QGIS "Near"

Nicklas Avén nicklas.aven at jordogskog.no
Thu May 27 11:57:03 PDT 2010


Hallo

>From Esri perspective I don't think you should consider it basic functionalitysince it requires ArcInfo license. 

But I agree that the functionality included in this Near function isquite basic. But there is a lot of different functionality mixed up inthe function.

If you are storing your data in PostGIS you can do it there. If youdon't... it might be a great idea to do :-)

To get the search radius you use ST_Dwithin. Then you can order thecatched features with ST_Distance and from your resulting nearestfeatures you can get all the resulting data that is included in thisNear function by using different of functions

Here comes an example where I have tried to do it as similar as the nearfunction as possible.
You can run it here:
http://www.postgisonline.org/map.php
The query searches for the houses closest to the three phone_masts. Toeasier see what it is about you can choose "All_simple_maps" underbackground maps.
The resulting map will just show a line between the phone mast and theclosest house and then you can find the attribute data if you press"Attribute table Map1"

This near_x and near_y I don't really understand. On the pictures itlooks like they are dealing with delta x and delta y but in the textthey are just discussing the x and y value of the found closest point.Both variants is easy to do but here I calculate the delta x and deltay.

The query looks more messy than nessecary because I have squized in allthat is included in near function. In reality you just specify the partsyou need of course. 

Here it comes, just copy it into the big textarea and press "Map 1" andchoose background map as desribed above


SELECT  
mastid,
houseid as fid,
ST_Distance(ageom, bgeom) as near_dist,
ST_X(ST_Endpoint(shortline))-ST_X(ST_Startpoint(shortline)) as near_x,
ST_Y(ST_Endpoint(shortline))-ST_Y(ST_Startpoint(shortline)) as near_y,
 ST_Azimuth(ST_Startpoint(shortline),ST_Endpoint(shortline))/(2*pi())*360as near_angle,
shortline as the_geom /*just for showing the line on the map*/
from 
(
    SELECT distinct on (mastid) mastid, houseid,  a.the_geom as ageom,b.the_geom as bgeom, ST_Shortestline(a.the_geom, b.the_geom) asshortline 
    FROM phone_masts a, houses b 
    WHERE ST_DWithin(a.the_geom, b.the_geom, 50000) 
    ORDER BY a.mastid, ST_Distance(a.the_geom, b.the_geom)
) subq;

I wrote it with a subquery to save some rewriting.

then to use it in QGIS you just put 
create table a_new_fancy_table_name as
in front of the query and run it all together and to get a unique id you afterwards run

alter table your_new_fancy_table_name add column gid serial unique;

then you will get a new column automatically filled. You also get a constraint that tells that the value in the field has to be unique.


Cheers
Nicklas


2010-05-26 whollycow  wrote:

Is anyone aware of a function equivalent to "Near" in ArcGIS?(see webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Near_(Analysis) for reference)I have been searching all day and haven't come across anything quite like it.  It seems like such basic functionality I would be surprised if someone hasn't already coded a solution.
>View this message in context: QGIS "Near"
>Sent from the qgis-user mailing list archive at Nabble.com.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20100527/00ea5e0e/attachment.html>


More information about the Qgis-user mailing list