-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth_data.py
More file actions
31 lines (26 loc) · 1.18 KB
/
Copy pathtruth_data.py
File metadata and controls
31 lines (26 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gdal
import ogr
import numpy as np
import geopandas as gpd
import pandas as pd
# read shapefile to geopandas geodataframe
gdf = gpd.read_file('E://arc//sds//seg//truth_data_subset_utm12.shp')
# get names of land cover classes/labels
class_names = gdf['lctype'].unique()
print('class_name', class_names)
# create a unique id (integer) for each land cover class/label
class_ids =np.arange(class_names.size)+1
print('class ids',class_ids)
# create a pandas data frame of the labels and ids and save to cs0\
df = pd.DataFrame({'lctype': class_names, 'id': class_ids})
df.to_csv('E://arc//sds//seg//class_lookup.csv')
print('gdf without ids', gdf.head())
# add a new column to geodatafame with the id for each class/label
gdf['id'] = gdf['lctype'].map(dict(zip(class_names, class_ids)))
print('gdf with ids', gdf.head())
# split the truth data into training and test data sets and save each to a new shapefile
gdf_train = gdf.sample(frac=0.7)
gdf_test = gdf.drop(gdf_train.index)
print('gdf shape', gdf.shape, 'training shape', gdf_train.shape, 'test', gdf_test.shape)
gdf_train.to_file('E://arc//sds//seg//train.shp')
gdf_test.to_file('E://arc//sds//seg//test.shp')