0

I have two shapefiles with points distributed over the whole world. I would like to measure the distance (like the crow flies in km) from each point in shapefile 1 to the closest point from shapefile 2, add all the attributes from the closest point of shapefile2 to the point in shapefile1 but at the same time I have to make sure that these distances are only calculated within the same country. The distance measurement should not cross any borders.

I tried to use the Near option and it gives me partly what I want. It adds all the attributes from the closest point in shapefile2 to the closest point in shapefile1 plus it measures the distance. Now the only problem is that it does not take into account if the two closest points are also in the same country. I would need something like an "if" option for the Near tool. Something like "measure distance to the closest point if country in shapefile 1 = country in shapefile 2". Is there something like that??

Any suggestions?

  • please expand your question to include how you want these distances (format, storage type, PCS [projection], unit, etc). Also explain what you have tried. You are more likely to get help moving along than for someone to set up the entire project for you. Just be sure to edit your original question. and refer to the help FAQ if you need to. Also reading question that already have answers could help. – Brad Nesom May 26 '15 at 15:54
  • Please clarify what you mean by "the distance measurement should not cross any borders". A graphical representation (picture) would not be inappropriate. – Vince May 26 '15 at 16:01
  • 1
    What if two points are in the same country but the shortest path crosses the boundaries ? – radouxju May 26 '15 at 16:04
  • 1
    "the distance measurement should not cross any borders", sorry for that sentence it is really unclear. I mean that the distance should be measured between the two closest points within the same country, ignoring any point in a neighbouring country even if the point in the neighbouring country is closer. – Michael May 26 '15 at 18:08
  • "What if two points are in the same country but the shortest path crosses the boundaries?" This doesn't matter in my setting as long as both points are in the same country. – Michael May 26 '15 at 18:12
1

This is more or less a complete rewrite of my original answer; I was incorrect about what I thought the OP was asking. Given some of the additional information, I would recommend doing something like the following. You could enter it in the python console, or just make a standalone script on the desktop and run it with idle.

import arcpy
arcpy.env.overwrite = True

countries = r"path/to/countries.shp"
pointsA = r"path/to/pointsA.shp"
pointsB = r"path/to/pointsB.shp"

## make feature layers for these datasets
coun = arcpy.management.MakeFeatureLayer(countries,"coun_fl")
ptA = arcpy.management.MakeFeatureLayer(pointsA,"ptA_fl")
ptB = arcpy.management.MakeFeatureLayer(pointsB,"ptB_fl")

## get FIDs of countries that actually intersect with these points
arcpy.management.SelectLayerByLocation(coun,"NEW_SELECTION",ptA)
arcpy.management.SelectLayerByLocation(coun,"ADD_TO_SELECTION",ptB)
fids = [r[0] for r in arcpy.da.SearchCursor(coun,"FID")]

## iterate through FID list, select each country, and then select points
for fid in fids:
    sql = '"FID" = {0}'.format(fid)
    arcpy.management.SelectLayerByAttribute(coun,"NEW_SELECTION",sql)
    arcpy.management.SelectLayerByLocation(ptA,"NEW_SELECTION",coun)
    arcpy.management.SelectLayerByLocation(ptB,"NEW_SELECTION",coun)

    ## now you have only selected points that fall within the same country
    ## you can do whatever operation you want, like
    arcpy.analysis.Near(ptA,ptB)

I don't know how many points you are dealing with, so I'm not sure if this is the speediest way to accomplish what you need. The select processes can be sloooowww sometimes.

You'll still need to check out the projected coordinate systems you use.

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.