Add centroid fields & Convert to a DF
This commit is contained in:
parent
042913acdc
commit
7242159aaa
|
@ -7,24 +7,71 @@ Project Developer: Alireza Adli alireza.adli@concordia.ca
|
||||||
# You need to clone mtl_gis_oo project and
|
# You need to clone mtl_gis_oo project and
|
||||||
# add it as a dependency of this new project
|
# add it as a dependency of this new project
|
||||||
from scrub_layer_class import *
|
from scrub_layer_class import *
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
# Change the paths by the location of your QGIS installation and datalayers
|
# Change the paths by the location of your QGIS installation and datalayers
|
||||||
qgis_path = 'C:/Program Files/QGIS 3.34.1/apps/qgis'
|
qgis_path = 'C:/Program Files/QGIS 3.34.1/apps/qgis'
|
||||||
|
|
||||||
varennes_nrcan_extra_polygons = \
|
varennes_nrcan_extra_polygons = \
|
||||||
'C:/Users/a_adli/PycharmProjects/varennes_gis_oo/' \
|
'C:/Users/a_adli/PycharmProjects/varennes_gis_oo/' \
|
||||||
'data/initial_data/endeavor/nrcan_centroid_main/nrcan_centroid_main.shp'
|
'data/initial_data/endeavor/nrcan_without_centroids/auto_building_2.shp'
|
||||||
|
|
||||||
# first we duplicate the layer to preserve the main data layer.
|
# First we duplicate the layer to preserve the main data layer.
|
||||||
duplicated = \
|
duplicated = \
|
||||||
'C:/Users/a_adli/PycharmProjects/varennes_gis_oo/' \
|
'C:/Users/a_adli/PycharmProjects/varennes_gis_oo/' \
|
||||||
'data/initial_data/endeavor/nrcan_centroids_1/auto_building_2.shp'
|
'data/initial_data/endeavor/nrcan_centroids/nrcan_centroids.shp'
|
||||||
|
|
||||||
# First, the layer will be duplicated as some records are going to be removed
|
|
||||||
|
|
||||||
varennes_nrcan = ScrubLayer(
|
varennes_nrcan = ScrubLayer(
|
||||||
qgis_path, varennes_nrcan_extra_polygons, 'NRCan Varennes')
|
qgis_path, varennes_nrcan_extra_polygons, 'NRCan Varennes')
|
||||||
|
varennes_nrcan.duplicate_layer(duplicated)
|
||||||
|
|
||||||
|
varennes_nrcan_centroids = ScrubLayer(
|
||||||
|
qgis_path, duplicated, 'NRCan Varennes with Coordinates')
|
||||||
|
|
||||||
|
# Then we add coordinates to each polygon so we can remove the
|
||||||
|
# very similar polygons based on coordinates.
|
||||||
|
varennes_nrcan_centroids.layer.startEditing()
|
||||||
|
|
||||||
|
# Add new fields for the centroid coordinates
|
||||||
|
varennes_nrcan_centroids.layer.dataProvider().\
|
||||||
|
addAttributes([QgsField("centroid_x", QVariant.Double),
|
||||||
|
QgsField("centroid_y", QVariant.Double)])
|
||||||
|
varennes_nrcan_centroids.layer.updateFields()
|
||||||
|
|
||||||
|
|
||||||
|
centroid_x_index = varennes_nrcan_centroids.\
|
||||||
|
layer.fields().indexFromName("centroid_x")
|
||||||
|
centroid_y_index = varennes_nrcan_centroids.\
|
||||||
|
layer.fields().indexFromName("centroid_y")
|
||||||
|
|
||||||
|
for feature in varennes_nrcan_centroids.layer.getFeatures():
|
||||||
|
centroid = feature.geometry().centroid().asPoint()
|
||||||
|
feature.setAttribute(centroid_x_index, centroid.x())
|
||||||
|
feature.setAttribute(centroid_y_index, centroid.y())
|
||||||
|
varennes_nrcan_centroids.layer.updateFeature(feature)
|
||||||
|
|
||||||
|
# Commit the changes for adding centroids fields
|
||||||
|
varennes_nrcan_centroids.layer.commitChanges()
|
||||||
|
|
||||||
|
|
||||||
|
# Pandas is a better option to compare polygons and remove the duplicates
|
||||||
|
# so we make a dataframe. We just transfer the necessary fields
|
||||||
|
# to the dataframe.
|
||||||
|
|
||||||
|
field_names = ['feature_id', 'centroid_x', 'centroid_y']
|
||||||
|
|
||||||
|
# Get the indices of the specified fields
|
||||||
|
field_indices = [varennes_nrcan_centroids.layer.fields().indexOf(field)
|
||||||
|
for field in field_names]
|
||||||
|
|
||||||
|
# Extract the attribute values and store them in a list of dictionaries
|
||||||
|
data = []
|
||||||
|
for feature in varennes_nrcan_centroids.layer.getFeatures():
|
||||||
|
attributes = [feature.attributes()[index] for index in field_indices]
|
||||||
|
data.append(dict(zip(field_names, attributes)))
|
||||||
|
|
||||||
|
# Create a DataFrame from the list of dictionaries
|
||||||
|
varennes_nrcan_centroids_df = pd.DataFrame(data)
|
||||||
|
varennes_nrcan_centroids_df['ID'] = range(len(varennes_nrcan_centroids_df))
|
||||||
|
|
||||||
varennes_nrcan_duplicate = varennes_nrcan.duplicate_layer(
|
|
||||||
duplicated, 'NRCan duplicated')
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user