Announcement

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

  • Reverse geocoding with geoinpoly gives inaccurate results help

    Hello,

    I am trying to use geoinpoly to extract countries from coordinates. I am using the 'PDSI - Monthly Mean: self-calibrated' data from NOAA, which can be found here: https://psl.noaa.gov/data/gridded/data.pdsi.html#detail. I then use Panoply.exe to extract the netcdf file to a .csv file, which is then imported into stata and has a pair of latitude and longitude coordinates. I keep only the coordinates where the key variable (pdsi) is not missing.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double date float(lat lon pdsi)
    4.733856e+11 -53.75 -73.75  -.8622768
    4.733856e+11 -53.75 -71.25  -4.905787
    4.733856e+11 -53.75 -68.75  -2.884902
    4.733856e+11 -53.75 -66.25  -.5601953
    4.733856e+11 -51.25 -73.75  -.6036547
    4.733856e+11 -51.25 -71.25  -3.588632
    4.733856e+11 -51.25 -58.75 -1.3314766
    4.733856e+11 -48.75 -76.25  -.1892304
    4.733856e+11 -48.75 -73.75 -.56877613
    4.733856e+11 -48.75 -71.25 -2.1497626
    4.733856e+11 -48.75 -68.75   -2.14897
    4.733856e+11 -48.75 -66.25 -1.9930106
    4.733856e+11 -48.75  68.75  -.3168081
    4.733856e+11 -46.25 -76.25 -4.6985755
    4.733856e+11 -46.25 -73.75  -5.249196
    4.733856e+11 -46.25 -71.25 -3.9030654
    4.733856e+11 -46.25 -68.75 -1.0852239
    4.733856e+11 -46.25 166.25  -3.616439
    4.733856e+11 -46.25 168.75 -3.9492075
    4.733856e+11 -43.75 -73.75    -5.1826
    4.733856e+11 -43.75 -71.25 -4.5318255
    4.733856e+11 -43.75 -68.75  -2.567297
    4.733856e+11 -43.75 -66.25 -1.0807338
    4.733856e+11 -43.75 146.25   2.757163
    4.733856e+11 -43.75 168.75  -.8358558
    4.733856e+11 -43.75 171.25 -1.3733033
    4.733856e+11 -43.75 173.75  2.0661316
    4.733856e+11 -41.25 -73.75  -3.731872
    4.733856e+11 -41.25 -71.25  -2.983261
    4.733856e+11 -41.25 -68.75  1.4202592
    4.733856e+11 -41.25 -66.25  1.3901818
    4.733856e+11 -41.25 -63.75   .7631465
    4.733856e+11 -41.25 143.75  2.2810497
    4.733856e+11 -41.25 146.25   2.860601
    4.733856e+11 -41.25 171.25  -1.000528
    4.733856e+11 -41.25 173.75  1.5783185
    4.733856e+11 -41.25 176.25 -1.3318152
    4.733856e+11 -38.75 -73.75  -3.987925
    4.733856e+11 -38.75 -71.25 -2.2837498
    4.733856e+11 -38.75 -68.75   2.637683
    4.733856e+11 -38.75 -66.25    3.00341
    4.733856e+11 -38.75 -63.75   .8945818
    4.733856e+11 -38.75 -61.25  .07772385
    4.733856e+11 -38.75 -58.75 -2.3103743
    4.733856e+11 -38.75 141.25  1.9664747
    4.733856e+11 -38.75 143.75   -.532143
    4.733856e+11 -38.75 146.25  2.3114488
    4.733856e+11 -38.75 148.75   3.021299
    4.733856e+11 -38.75 173.75  -.9413062
    4.733856e+11 -38.75 176.25  -.7882169
    4.733856e+11 -36.25 -73.75 -3.6424384
    4.733856e+11 -36.25 -71.25 -1.7606176
    4.733856e+11 -36.25 -68.75   2.042183
    4.733856e+11 -36.25 -66.25  1.9240352
    4.733856e+11 -36.25 -63.75  1.0326267
    4.733856e+11 -36.25 -61.25  .11875542
    4.733856e+11 -36.25 -58.75 -2.3584473
    4.733856e+11 -36.25 136.25  2.1486964
    4.733856e+11 -36.25 138.75   3.532063
    4.733856e+11 -36.25 141.25   3.095695
    4.733856e+11 -36.25 143.75   3.218915
    4.733856e+11 -36.25 146.25  -.4469681
    4.733856e+11 -36.25 148.75   2.965404
    4.733856e+11 -36.25 173.75  -1.857902
    4.733856e+11 -33.75 -71.25    .330117
    4.733856e+11 -33.75 -68.75  1.1416228
    4.733856e+11 -33.75 -66.25  1.5281847
    4.733856e+11 -33.75 -63.75    .716531
    4.733856e+11 -33.75 -61.25  -2.745389
    4.733856e+11 -33.75 -58.75  -2.730287
    4.733856e+11 -33.75 -56.25 -1.8756796
    4.733856e+11 -33.75 -53.75 -2.0038304
    4.733856e+11 -33.75  18.75   3.066416
    4.733856e+11 -33.75  21.25   .4042225
    4.733856e+11 -33.75  23.75 -.51310474
    4.733856e+11 -33.75  26.25   3.193037
    4.733856e+11 -33.75  28.75  -1.773607
    4.733856e+11 -33.75 116.25 -1.5471634
    4.733856e+11 -33.75 118.75  -.2362924
    4.733856e+11 -33.75 121.25  -2.767646
    4.733856e+11 -33.75 123.75 -1.6566436
    4.733856e+11 -33.75 133.75 -.06925482
    4.733856e+11 -33.75 136.25  -.2209172
    4.733856e+11 -33.75 138.75   .3293644
    4.733856e+11 -33.75 141.25  .09760648
    4.733856e+11 -33.75 143.75   .1191175
    4.733856e+11 -33.75 146.25   3.368574
    4.733856e+11 -33.75 148.75  -.8078451
    4.733856e+11 -33.75 151.25  -.9469776
    4.733856e+11 -31.25 -71.25 -.22133857
    4.733856e+11 -31.25 -68.75  2.0080686
    4.733856e+11 -31.25 -66.25  1.6745914
    4.733856e+11 -31.25 -63.75  1.4677007
    4.733856e+11 -31.25 -61.25  -.3303558
    4.733856e+11 -31.25 -58.75 -1.0560408
    4.733856e+11 -31.25 -56.25  -.5111065
    4.733856e+11 -31.25 -53.75  -.9953581
    4.733856e+11 -31.25 -51.25  -.9996645
    4.733856e+11 -31.25  16.25  -.2985082
    4.733856e+11 -31.25  18.75   2.862094
    end
    format %tc date
    I then use geoinpoly to extract countries from coordinates with the TM_WORLD_BORDERS-0.3 shapefile. The countries of interest are Australia, Belgium , Canada, Switzerland, Germany, Denmark, Finland, France, United Kingdom, Greece, Israel, Japan, Netherlands, New Zealand, Portugal, Brazil, Chile, China, Indonesia, India, South Korea, Mexico, Malaysia, Peru, Phillipines, Poland, Russia, Thailand, Turkey, and South Africa.

    There are results for all countries except for Netherlands, which is a key country in my dataset. I am wondering if it is a problem with overlapping polygons and how to fix this? I have shown a picture below which was created in QGIS. The shaded squares represent the pdsi datapoints from the netcdf file and the colored map is the shapefile. As you can see, the squares around the Netherlands are not completely within the country's borders. However, I think these points should still be considered as a data point within the Netherlands. Additionally, most of the pdsi data points per country do not match the original sample I am trying to recreate, so I am not sure what to do. Any help or guidance on this issue would be greatly appreciated.

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	91.1 KB
ID:	1635730

  • #2
    Bob Honda I am not sure fully understand since
    1. All your example data points are in the Southern hemisphere (see example below). Though some outside of country boundaries.
    2. "The shaded squares represent the pdsi datapoints" - What does this mean? The center point of each square? If so, then it is possible the data point could be outside the polygons.
    3. Using a set of points in the Netherlands -geoinpoly- correctly identifies them (example 2, below).
    Code:
    clear
    input double date float(lat lon pdsi)
    4.733856e+11 -53.75 -73.75  -.8622768
    4.733856e+11 -53.75 -71.25  -4.905787
    4.733856e+11 -53.75 -68.75  -2.884902
    4.733856e+11 -53.75 -66.25  -.5601953
    4.733856e+11 -51.25 -73.75  -.6036547
    4.733856e+11 -51.25 -71.25  -3.588632
    4.733856e+11 -51.25 -58.75 -1.3314766
    4.733856e+11 -48.75 -76.25  -.1892304
    4.733856e+11 -48.75 -73.75 -.56877613
    4.733856e+11 -48.75 -71.25 -2.1497626
    4.733856e+11 -48.75 -68.75   -2.14897
    4.733856e+11 -48.75 -66.25 -1.9930106
    4.733856e+11 -48.75  68.75  -.3168081
    4.733856e+11 -46.25 -76.25 -4.6985755
    4.733856e+11 -46.25 -73.75  -5.249196
    4.733856e+11 -46.25 -71.25 -3.9030654
    4.733856e+11 -46.25 -68.75 -1.0852239
    4.733856e+11 -46.25 166.25  -3.616439
    4.733856e+11 -46.25 168.75 -3.9492075
    4.733856e+11 -43.75 -73.75    -5.1826
    4.733856e+11 -43.75 -71.25 -4.5318255
    4.733856e+11 -43.75 -68.75  -2.567297
    4.733856e+11 -43.75 -66.25 -1.0807338
    4.733856e+11 -43.75 146.25   2.757163
    4.733856e+11 -43.75 168.75  -.8358558
    4.733856e+11 -43.75 171.25 -1.3733033
    4.733856e+11 -43.75 173.75  2.0661316
    4.733856e+11 -41.25 -73.75  -3.731872
    4.733856e+11 -41.25 -71.25  -2.983261
    4.733856e+11 -41.25 -68.75  1.4202592
    4.733856e+11 -41.25 -66.25  1.3901818
    4.733856e+11 -41.25 -63.75   .7631465
    4.733856e+11 -41.25 143.75  2.2810497
    4.733856e+11 -41.25 146.25   2.860601
    4.733856e+11 -41.25 171.25  -1.000528
    4.733856e+11 -41.25 173.75  1.5783185
    4.733856e+11 -41.25 176.25 -1.3318152
    4.733856e+11 -38.75 -73.75  -3.987925
    4.733856e+11 -38.75 -71.25 -2.2837498
    4.733856e+11 -38.75 -68.75   2.637683
    4.733856e+11 -38.75 -66.25    3.00341
    4.733856e+11 -38.75 -63.75   .8945818
    4.733856e+11 -38.75 -61.25  .07772385
    4.733856e+11 -38.75 -58.75 -2.3103743
    4.733856e+11 -38.75 141.25  1.9664747
    4.733856e+11 -38.75 143.75   -.532143
    4.733856e+11 -38.75 146.25  2.3114488
    4.733856e+11 -38.75 148.75   3.021299
    4.733856e+11 -38.75 173.75  -.9413062
    4.733856e+11 -38.75 176.25  -.7882169
    4.733856e+11 -36.25 -73.75 -3.6424384
    4.733856e+11 -36.25 -71.25 -1.7606176
    4.733856e+11 -36.25 -68.75   2.042183
    4.733856e+11 -36.25 -66.25  1.9240352
    4.733856e+11 -36.25 -63.75  1.0326267
    4.733856e+11 -36.25 -61.25  .11875542
    4.733856e+11 -36.25 -58.75 -2.3584473
    4.733856e+11 -36.25 136.25  2.1486964
    4.733856e+11 -36.25 138.75   3.532063
    4.733856e+11 -36.25 141.25   3.095695
    4.733856e+11 -36.25 143.75   3.218915
    4.733856e+11 -36.25 146.25  -.4469681
    4.733856e+11 -36.25 148.75   2.965404
    4.733856e+11 -36.25 173.75  -1.857902
    4.733856e+11 -33.75 -71.25    .330117
    4.733856e+11 -33.75 -68.75  1.1416228
    4.733856e+11 -33.75 -66.25  1.5281847
    4.733856e+11 -33.75 -63.75    .716531
    4.733856e+11 -33.75 -61.25  -2.745389
    4.733856e+11 -33.75 -58.75  -2.730287
    4.733856e+11 -33.75 -56.25 -1.8756796
    4.733856e+11 -33.75 -53.75 -2.0038304
    4.733856e+11 -33.75  18.75   3.066416
    4.733856e+11 -33.75  21.25   .4042225
    4.733856e+11 -33.75  23.75 -.51310474
    4.733856e+11 -33.75  26.25   3.193037
    4.733856e+11 -33.75  28.75  -1.773607
    4.733856e+11 -33.75 116.25 -1.5471634
    4.733856e+11 -33.75 118.75  -.2362924
    4.733856e+11 -33.75 121.25  -2.767646
    4.733856e+11 -33.75 123.75 -1.6566436
    4.733856e+11 -33.75 133.75 -.06925482
    4.733856e+11 -33.75 136.25  -.2209172
    4.733856e+11 -33.75 138.75   .3293644
    4.733856e+11 -33.75 141.25  .09760648
    4.733856e+11 -33.75 143.75   .1191175
    4.733856e+11 -33.75 146.25   3.368574
    4.733856e+11 -33.75 148.75  -.8078451
    4.733856e+11 -33.75 151.25  -.9469776
    4.733856e+11 -31.25 -71.25 -.22133857
    4.733856e+11 -31.25 -68.75  2.0080686
    4.733856e+11 -31.25 -66.25  1.6745914
    4.733856e+11 -31.25 -63.75  1.4677007
    4.733856e+11 -31.25 -61.25  -.3303558
    4.733856e+11 -31.25 -58.75 -1.0560408
    4.733856e+11 -31.25 -56.25  -.5111065
    4.733856e+11 -31.25 -53.75  -.9953581
    4.733856e+11 -31.25 -51.25  -.9996645
    4.733856e+11 -31.25  16.25  -.2985082
    4.733856e+11 -31.25  18.75   2.862094
    end
    format %tc date
    clear
    copy http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip . , replace
    unzipfile tm_world_borders-0.3.zip, replace
    spshape2dta tm_world_borders-0.3.dta, replace
    use  tm_world_borders-0.3.dta,clear
    
    use lat_lon,clear
    geoinpoly lat lon using tm_world_borders-0.3_shp.dta
    
    merge m:1 _ID using tm_world_borders-0.3.dta
    tab NAME if _merge == 3
    
    use  tm_world_borders-0.3.dta,clear
    spmap using tm_world_borders-0.3_shp.dta , id(_ID) osize(vthin) point(data(lat_lon) xco(lon) yco(lat)  shape(Oh) ocolor(gs5) )
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	142.0 KB
ID:	1635929


    Code:
    //Random points in the Netherlands
    clear
    input double lat lon
    53.082444  4.805727
    52.808635   6.299650
    51.978508  6.379969
    53.451264  5.833546
    end
    //Project Points
    
    save lat_lon_netherlands,replace
    geoinpoly lat lon using tm_world_borders-0.3_shp.dta
    merge m:1 _ID using tm_world_borders-0.3.dta
    tab NAME if _merge == 3
    
    use tm_world_borders-0.3_shp.dta,clear
    geo2xy _Y _X, replace
    save world_borders_proj,replace
    use lat_lon_netherlands
    geo2xy lat lon, replace
    sav,replace
    
    use  tm_world_borders-0.3.dta,clear
    summarize SUBREGION if NAME == "Netherlands"
    keep if SUB  == r(mean)
    spmap using  world_borders_proj.dta , id(_ID) osize(vthin) point(data(lat_lon_netherlands) xco(lon) yco(lat)  shape(O) fcolor(blue) size(*.5))
    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	34.5 KB
ID:	1635930

    Comment


    • #3
      Scott Merryman Thank you very much for you help.

      In regard to your questions:

      1. I have uploaded the dataset I am using as a .dta file, which contains all wanted coordinate points, which should include the countries listed above as well as others.
      2. I have checked the data points and the pdsi data points are located within the center of the squares. I have attached a picture to illustrate the data points below.

      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	122.6 KB
ID:	1635950


      Based on the answer for the 2nd questions, it seems that the data points are outside of the polygons, how would you recommend to fix this issue?

      Attached Files

      Comment


      • #4
        Perhaps you could use -geoinpoly- to assign the pdsi data to the nearest polygon.

        Comment

        Working...
        X