Dan_Patterson

Densification.... sometimes being dense is a good thing

Blog Post created by Dan_Patterson Champion on Jan 18, 2018

Yup... adding points to poly* features

Already offered... Densify at all license levels?  NO!

My missive .... Densification by Factor  YES!

 

 

There is also a Geodesic Densify, but I don't do Geographic in any event, so use esri tools

Why did I reinvent the wheel?  Because the densification basis is to simply provide a sequence of locations, for X and Y then subdivide the intervening location by some factor (hence the ... by Factor)

 

Here is an example....

a = np.array([[ 20.,  20.],
              [ 20.,  30.],
              [ 30.,  30.],
              [ 30.,  20.],
              [ 20.,  20.]])

 

Then you run some code

def _densify_2D(a, fact=2):
    """Densify a 2D array using np.interp.
    """

    # Y = a changed all the y's to a
    a = np.squeeze(a)
    n_fact = len(a) * fact
    b = np.arange(0, n_fact, fact)
    b_new = np.arange(n_fact - 1)
    c0 = np.interp(b_new, b, a[:, 0])
    c1 = np.interp(b_new, b, a[:, 1])
    n = c0.shape[0]
    c = np.zeros((n, 2))
    c[:, 0] = c0
    c[:, 1] = c1
    return c

And you get this.

a_dens = _densify_2D(a, fact=2)

a_dens

array([[ 20.,  20.],
       [ 20.,  25.],
       [ 20.,  30.],
       [ 25.,  30.],
       [ 30.,  30.],
       [ 30.,  25.],
       [ 30.,  20.],
       [ 25.,  20.],
       [ 20.,  20.]])
a.shape  # (49874, 2)

%timeit _densify_2D(a, fact=2)

5.25 ms ± 323 µs per loop
     (mean ± std. dev. of 7 runs,
      100 loops each)

 

 

Where it really shines, is for long features that just need to be densified without any particular requirement for a set spacing.  If is fairly speedy as well.

Hope someone finds this useful, if you don't have the Standard or Advanced license in particular.

Outcomes