Verify if a point is Land or Water in Google Maps

后端 未结 18 1873
不知归路
不知归路 2020-11-22 17:16

..and then Google-maps \"divide the waters from the waters\"

Well, not in the biblical sense but..

I would like to know what options I have in order to verif

18条回答
  •  日久生厌
    2020-11-22 17:52

    As a complete novice to Python I couldn't get SylvainB's solution to work with the python script that checks if coordinates are on land. I managed to figure it out however, by downloading OSGeo4W (https://trac.osgeo.org/osgeo4w/) and then installed everything I needed pip, Ipython, and checked that all the imports specified were there. I saved the following code as a .py file.

    Code to check if coordinates are on land

    ###make sure you check these are there and working separately before using the .py file 
    
    import ogr
    from IPython import embed
    from osgeo import osr
    import osgeo
    
    import random
    #####generate a 1000 random coordinates
    ran1= [random.uniform(-180,180) for x in range(1,1001)]
    ran2= [random.uniform(-180,180) for x in range(1,1001)]
    
    
    drv = ogr.GetDriverByName('ESRI Shapefile') #We will load a shape file
    ds_in = drv.Open("D:\Downloads\land-polygons-complete-4326\land-polygons-complete-4326\land_polygons.shp")    #Get the contents of the shape file
    lyr_in = ds_in.GetLayer(0)    #Get the shape file's first layer
    
    #Put the title of the field you are interested in here
    idx_reg = lyr_in.GetLayerDefn().GetFieldIndex("P_Loc_Nm")
    
    #If the latitude/longitude we're going to use is not in the projection
    #of the shapefile, then we will get erroneous results.
    #The following assumes that the latitude longitude is in WGS84
    #This is identified by the number "4236", as in "EPSG:4326"
    #We will create a transformation between this and the shapefile's
    #project, whatever it may be
    geo_ref = lyr_in.GetSpatialRef()
    point_ref=osgeo.osr.SpatialReference()
    point_ref.ImportFromEPSG(4326)
    ctran=osgeo.osr.CoordinateTransformation(point_ref,geo_ref)
    ###check if the random coordinates are on land
    def check(runs):
        lon=ran1[runs]
        lat=ran2[runs]
        #Transform incoming longitude/latitude to the shapefile's projection
        [lon,lat,z]=ctran.TransformPoint(lon,lat)
        #Create a point
        pt = ogr.Geometry(ogr.wkbPoint)
        pt.SetPoint_2D(0, lon, lat)
        #Set up a spatial filter such that the only features we see when we
        #loop through "lyr_in" are those which overlap the point defined above
        lyr_in.SetSpatialFilter(pt)
        #Loop through the overlapped features and display the field of interest
        for feat_in in lyr_in:
            return(lon, lat)
    
    ###give it a try
    result = [check(x) for x in range(1,11)] ###checks first 10 coordinates
    

    I tried to get it to work in R but I had a nightmare trying to get all the packages you need to install so stuck to python.

提交回复
热议问题