4
votes

I have a NumPy array of latitude/longitude (WGS84) coordinates like this:

coords = np.asarray([
    [6.74219, -53.57835],
    [6.74952, -53.57241],
    [6.75652, -53.56289],
    [6.74756, -53.56598],
    [6.73462, -53.57518]])

I want to use the pyproj library in order to project these lat/lon coordinates using a Winkel-Tripel (still WGS84) without processing each point separately?

How can I join the result of pyproj, which are separate X/Y arrays, into a single numpy array just like the coordinate array above?

1

1 Answers

3
votes

The pyproj documentation shows how to project single points and

For your projection in WGS84, you can use the following function:

def project_array(coordinates, srcp='latlong', dstp='wintri'):
    """
    Project a numpy (n,2) array in projection srcp to projection dstp
    Returns a numpy (n,2) array.
    """
    p1 = pyproj.Proj(proj=srcp, datum='WGS84')
    p2 = pyproj.Proj(proj=dstp, datum='WGS84')
    fx, fy = pyproj.transform(p1, p2, coordinates[:,0], coordinates[:,1])
    # Re-create (n,2) coordinates
    return np.dstack([fx, fy])[0]

Note the use of dstack to join the x/y arrays

Usage example with your coordinate arrays.

>>> project_array(coords)
array([[  497789.36471653, -5965577.60559519],
       [  498357.98167095, -5964919.091806  ],
       [  498918.88707764, -5963861.91844427],
       [  498243.057953  , -5964202.54601671],
       [  497245.19767552, -5965221.87480737]])