Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Closest values for latitude longitude

    Colleagues,

    I have the data set similar to the extract below:

    Code:
    input str7 postcode latitude longitude
    AB101XG    57.14416516    -2.114847768
    AB106RN    57.13787976    -2.121486688
    AB107JB    57.12427377    -2.127189644
    AB115QN    57.14270109    -2.093014619
    AB116UL    57.13754663    -2.112695886
    end
    I would like to match that data with the sample data provided in the extract below. In particular, I'm interested in matching values by the latitude and longitude values by assigning each postcode to the Geo* category where latitude and longitude indicate greatest proximity. Broadly speaking I'm looking to recreate functionality that is available via the Hub Distance tool in the MMQGIS plug-in in QGIS. With respect to this particular assignment, I would rather avoid working in QGIS as I'll be refreshing this data set so it's more efficient for me to have one do file that I can run when needed instead of joggling between software packages and merging the data.

    Code:
    input str4 outcode    latitude longitude
    GeoA    57.131086    -2.122482
    GeoB    57.13121    -2.082261
    GeoC    57.098381    -2.1724
    GeoD    57.108    -2.237
    GeoE    57.101    -2.27
    end
    I took the liberty of visualising the process in the figure below:

    Click image for larger version

Name:	geo matching.png
Views:	2
Size:	10.1 KB
ID:	215798


    The data is publicly available and was taken from the Free Map Tools website.
    Attached Files
    Last edited by Konrad Zdeb; 08 Sep 2014, 04:34.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    Given a data set of locations, and an external file of other locations (i.e., candidate "neighbors"), Robert Picard's -geonear- from -ssc- will find the k nearest neighbors for each point in the data set. It is very fast, even for large N. Will this do what you need?

    Regards, Mike

    Comment


    • #3
      I don't use QGIS but it sounds like you are looking for the nearest neighbors. This can easily be done with geonear from SSC. Here's an example with your data. Note that lat and lon should be double precision. I add a brute force example (requires geodist, also from SSC) but that quickly become impractical as the number of points increase. geonear is very fast, even with millions of points.

      Code:
      clear
      input str7 postcode double lat double lon
      AB101XG    57.14416516    -2.114847768
      AB106RN    57.13787976    -2.121486688
      AB107JB    57.12427377    -2.127189644
      AB115QN    57.14270109    -2.093014619
      AB116UL    57.13754663    -2.112695886
      end
      tempfile postcode
      save "`postcode'"
      
      clear
      input str4 outcode double lat double lon
      GeoA    57.131086    -2.122482
      GeoB    57.13121    -2.082261
      GeoC    57.098381    -2.1724
      GeoD    57.108    -2.237
      GeoE    57.101    -2.27
      end
      tempfile geo
      save "`geo'"
      
      * -------- using -geonear- from SSC --------------
      
      use "`postcode'", clear
      geonear postcode lat lon using "`geo'", n(outcode lat lon)
      list
      
      * -------- brute force approach --------------
      * requires -geodist- from SSC
      
      use "`postcode'", clear
      rename (lat lon) p=
      cross using "`geo'"
      geodist plat plon lat lon , gen(d) sphere
      
      sort postcode d
      by postcode: keep if _n == 1
      list

      Comment


      • #4
        Robert/Mike,

        Thank you very much for getting back to me and the helpful answers.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment

        Working...
        X