Python – Convert Geopandas Points to Numpy Array

geopandasnumpypythonpython 3

I have a geopackage with a point feature. Which contains points along 19 lines. So, the number of points per line is exactly the same. Therefore, I could see it as a matrix in numpy. Moreover, each point has an attribute "AffectedbyRiver", where 1 (white) and 0 (red). How can a extract that information from geopandas to numpy array as an array 204-by-19 ? I mean, as a matrix with zeros and ones.

Note: The figure below is a screenshot from QGIS3x

import geopandas as gpd
preplot = gpd.read_file('geodatabase.gpkg', layer='SourceRegularPoints')
preplot.plot(figsize=(12,8))

Link to geopackage

here is my code (manually):

indexline = [19,18,17,16,15,14,13,12,11,10,1,2,3,4,5,6,7,8,9]


array = np.zeros((204,19))
x_coor =  np.zeros((204,19))
y_coor = np.zeros((204,19))

for i in indexline:
    array[:,i-1]=preplot.loc[preplot['org_fid']==i].AffectedbyRiver.values
    x_coor[:,i-1]=preplot.loc[preplot['org_fid']==i].X.values
    y_coor[:,i-1]=preplot.loc[preplot['org_fid']==i].y.values

plt.scatter(x_coor.flatten(),y_coor.flatten(),c=array.flatten(),cmap='gray')

enter image description here

Best Answer

The only thing your code is missing is automatisation of indexline, I am correct? Just use the first point and its x coordinate in each line to determine the correct order to fill the arrays.

indexline = preplot[preplot['distance']==0].sort_values('X').org_fid

array = np.zeros((204,19))
x_coor =  np.zeros((204,19))
y_coor = np.zeros((204,19))

for i in indexline:
    array[:,i-1]=preplot.loc[preplot['org_fid']==i].AffectedbyRiver.values
    x_coor[:,i-1]=preplot.loc[preplot['org_fid']==i].X.values
    y_coor[:,i-1]=preplot.loc[preplot['org_fid']==i].y.values

If you also want to automatically determine the shape of the array, you can use this:

x = gdf['distance'].unique().shape[0]
y = gdf['org_fid'].unique().shape[0]

array = np.zeros((x, y))
x_coor =  np.zeros((x, y))
y_coor = np.zeros((x, y))
Related Question