Note
Click here to download the full example code
Read fields from vector¶
This example shows how to read fields values from a vector file.
Import librairies¶
import museotoolbox as mtb
Load HistoricalMap dataset¶
raster,vector = mtb.datasets.load_historical_data(low_res=True)
Note
If you have no memories on what the fields name are, simply put the vector path
try :
mtb.processing.read_vector_values(vector)
except Exception as e:
print(e)
Out:
These fields are available : ['Class', 'Type', 'uniquefid']
Read values from field ‘Class’¶
Y,Name = mtb.processing.read_vector_values(vector,'Class','Type')
print(Y,Name)
print(Y.shape)
Out:
[1 1 1 1 2 2 2 1 1 2 4 5 4 5 3 3 3] ['Forest' 'Forest' 'Forest' 'Forest' 'Agriculture' 'Agriculture'
'Agriculture' 'Forest' 'Forest' 'Agriculture' 'Water' 'Buildings' 'Water'
'Buildings' 'Soil' 'Soil' 'Soil']
(17,)
Read values from field beginning with ‘C’¶
As multiple fields can begin with C, function returns a column per field
C = mtb.processing.read_vector_values(vector,band_prefix='C')
print(C)
print(C.shape)
Out:
[[1]
[1]
[1]
[1]
[2]
[2]
[2]
[1]
[1]
[2]
[4]
[5]
[4]
[5]
[3]
[3]
[3]]
(17, 1)
from matplotlib import pyplot as plt
import numpy as np
plt.title('Number of polygons per label')
plt.bar(np.arange(np.unique(Y).size)+1,np.unique(Y,return_counts=True)[1])
Out:
<BarContainer object of 5 artists>
Total running time of the script: ( 0 minutes 0.094 seconds)