Calculating Point Density using Python

前端 未结 4 1844
花落未央
花落未央 2020-12-18 05:46

I have a list of X and Y coordinates from geodata of a specific part of the world. I want to assign each coordinate, a weight, based upon where it lies in the graph.

4条回答
  •  一生所求
    2020-12-18 06:17

    You initial inclination to draw a circle around each point and count the number of other points in that circle is a good one and as mentioned by unutbu, a KDTree will be a fast way to solve this problem.

    This can be done very easily with PySAL, which using scipy's kdtree under the hood.

    import pysal
    import numpy
    pts = numpy.random.random((100,2)) #generate some random points
    radius = 0.2 #pick an arbitrary radius
    
    #Build a Spatial Weights Matrix
    W = pysal.threshold_continuousW_from_array(pts, threshold=radius)
    # Note: if your points are in Latitude and Longitude you can increase the accuracy by
    #       passing the radius of earth to this function and it will use arc distances.
    # W = pysal.threshold_continuousW_from_array(pts, threshold=radius, radius=pysal.cg.RADIUS_EARTH_KM)
    
    print W.cardinalities
    #{0: 10, 1: 15, ..... }
    

    If your data is in a Shapefile, simply replace threshold_continuousW_from_array with threshold_continuousW_from_shapefile, see the docs for details.

提交回复
热议问题