0

I'm trying to create a GeoDataFrame points grid from a numpy.mgrid. I already have created the numpy.mgrid with coordinates of future points.

>>>arr = np.mgrid[-1827342.1753285176:-1827523.6181312904:121j,5328243.703187092:5328092.54873913:152j]

>>>arr

array([[[-1827342.17532852, -1827342.17532852, -1827342.17532852, ...,
     -1827342.17532852, -1827342.17532852, -1827342.17532852],
    [-1827343.68735187, -1827343.68735187, -1827343.68735187, ...,
     -1827343.68735187, -1827343.68735187, -1827343.68735187],
    [-1827345.19937523, -1827345.19937523, -1827345.19937523, ...,
     -1827345.19937523, -1827345.19937523, -1827345.19937523],
    ...,
    [-1827520.59408458, -1827520.59408458, -1827520.59408458, ...,
     -1827520.59408458, -1827520.59408458, -1827520.59408458],
    [-1827522.10610793, -1827522.10610793, -1827522.10610793, ...,
     -1827522.10610793, -1827522.10610793, -1827522.10610793],
    [-1827523.61813129, -1827523.61813129, -1827523.61813129, ...,
     -1827523.61813129, -1827523.61813129, -1827523.61813129]],

   [[ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913],
    [ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913],
    [ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913],
    ...,
    [ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913],
    [ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913],
    [ 5328243.70318709,  5328242.70216426,  5328241.70114142, ...,
      5328094.5507848 ,  5328093.54976196,  5328092.54873913]]])

After, I tried this method:

>>>pd.DataFrame(data=arr[0:,0:],    
                index=???,          
                columns=['x','y'])  

And after use x and y fields to create a POINT shapely geometry field. But I didn't find how to create an index.

So I tried to convert 2 parts oh the array in 2 lists like this:

>>>x = marr[0,0:].tolist()
>>>y = marr[1,0:].tolist()

But it doesn't really unpack the array.

Somebody have some tips?

0

The code below should help you out. Have a look at the comments to understand what it does.

import pandas as pd
import geopandas as gpd
import numpy as np
from shapely.geometry import Point

# create numpy mgrid
arr1 = np.mgrid[-1827342.1753285176:-1827523.6181312904:121j,5328243.703187092:5328092.54873913:152j]

# extract the x and y coordinates as flat arrays
arr1x = np.ravel(arr1[0])
arr1y = np.ravel(arr1[1])

# using the X and Y columns, build a dataframe, then the geodataframe
df1 = pd.DataFrame({'X':arr1x, 'Y':arr1y})
df1['coords'] = list(zip(df1['X'], df1['Y']))
df1['coords'] = df1['coords'].apply(Point)
gdf1 = gpd.GeoDataFrame(df1, geometry='coords')
  • Before you answered, I found a solution that's look like yours. But your 2nd part to build the DF is better. For the extraction of coordinates, I found this x, y = np.mgrid[-1827342.1753285176:-1827523.6181312904:121j,5328243.703187092:5328092.54873913:152j] ; arrx = np.ravel(x) ; arry = np.ravel(y) – Tim C. Jan 21 at 9:08
  • Glad it helped. If it answered your question, please accept it as the answer? – jberrio Jan 21 at 23:47
  • And to generate the np.mgrid with variables: x, y = np.mgrid[Xmin:Xmax:complex(cols),Ymin:Ymax:complex(rows)]where cols and rows are the number of columns and rows. complex(120) permet to recreate the value 120j. A simple concatenation of 120 and 'j' doesn't work. – Tim C. Jan 22 at 13:14

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.