2

I am using QGIS 2.16.

I have two different point layers:

  • points that represent students' homes
  • points that represent schools.

In both I have the school ID. I need to calculate the distance between students' homes and their schools, using school ID.

Any idea about a plugin or function that would enable to do this?

  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Nov 3 '16 at 20:24
4

Use the QGIS DB Manager and use SQL Spatial to join the data on the SCHOOL_ID column from both tables:

First load your shapefiles into the map:

enter image description here

Then open the Database > DBManager tool

Access your layers from the "Virtual Layers" node:

enter image description here

Click the Query (QGIS Layers) tab.

Here you can write some SQL and use the ST_Distance function as if you were running a PostGIS or Spatialite database.

Here is the query I can use to join the students to the schools on SCHOOLNUM / SCHNUM - and I can also filter by grade level, etc. etc.

I'm also storing my geometry in WGS84/4326, so I'm using the ST_Transform tool to project the geometry to STATE PLANE COLORADO / 2877 which is in Feet, which I then divide by 5280 to get my distances in MILES:

select 
sch.Schnum 
, stu.lname 
, ST_Distance(ST_Transform(sch.geometry, 2877), ST_Transform(stu.geometry, 2877)) / 5280 as DistToSchool_Mi

from "Schools_Current" as sch 
join "Geo_Active_Student" as stu on sch.schnum = stu.schoolnum
where stu.grade =  '05'
and sch.elem = '1'

And here is the result:

enter image description here

If you like how this works, you should get into PostGIS!!!

  • Thank you for your answer. I am trying to apply SQL to my case but I have some problems to do it. I will get into PostGIS to understand it. – Forsey Nov 3 '16 at 20:34
  • @Forsey feel free to contact me through the email in my profile - happy to help more with School GIS questions! – DPSSpatial Nov 3 '16 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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