Hi Pat,

Step 1 (already done): import csv as "Delimited Text Layer". Since there are no coordinates this results in a geometry-less layer.

Step 2: export and load the just imported csv as vector-layer (GeoPackage, Shape), geometry-type "Point", desired projection. This results in a Point-Layer with the same features and attributes as the csv but still without valid geometries.

Step 3: Update the empty geometries with valid points. Sadly there is a lack in QGis, edit existing geometries => OK, creating new features with geometries => OK,  update geometries in geometry-less features => not supported out-of-the-box.

So I (not Anthropic Claude 😉) wrote a script, attached as python file.

Operating instructions:

1: Load script into python console

2: activate the exported vector-layer from step 2

3. run the script

4. open attribute-table

5. select the desired feature in table

6. digitize the point-geometry for the selected feature by click on map

7. repeat steps 5 and 6 as often as you like

8. stop and save edits

Afterwards the features have valid geometries and can be edited with QGis on-board tools

Note:

The script does not write coordinates to the attribute-table but creates point-geometries. You can afterwards extract x- and y-coordinates into according fields by QGis-Field-Calculator.

hth

Ludwig

Am 02.12.24 um 14:08 schrieb Pat Brown via QGIS-User:
Hi
  I have a csv file with data about various mines. It includes descriptions of locality  but no coordinates. I have added this file to qgis and created a shapefile. I now want to add the coordinates of each mine by searching for its locality on a basemap in qgis then clicking on the map to add the coordinates to the shapefile attribute table.  Is this possible or do I have to search for each location on Google Earth, for example, then write out the coordinates and add them to the attribute table manually?

Thanks

Paddy

_______________________________________________
QGIS-User mailing list
QGIS-User@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
class MapToolUpdateNullGeometry(QgsMapToolEmitPoint):
    """
    Script to digitize point-geometries for features without geometries

    Operating instructions:
    1: Load script into python console
    2: activate point-layer
    3. run the script
    4. open attribute-table
    5. select the row for which you want to update the empty geometry
    6. digitize the point-geometry by click on map

    Repeat steps 5 and 6.
    
    Afterwards the features have valid geometries and can be edited as usual with QGis on-board tools
    """
    def __init__(self, iface, target_layer):
        QgsMapToolEmitPoint.__init__(self, iface.mapCanvas())
        
        self.iface = iface
        self.target_layer = target_layer
        
        if not self.target_layer.isEditable():
            self.target_layer.startEditing()
            
        self.qvm = QgsVertexMarker(iface.mapCanvas())
        self.qvm.setIconSize(10)
        self.qvm.setIconType(QgsVertexMarker.ICON_BOX)
        self.qvm.setPenWidth(3)
        self.qvm.setColor(QColor(0, 255, 0))
        
        # remove Marker
        self.deactivated.connect(lambda: iface.mapCanvas().scene().removeItem(self.qvm))


    def canvasMoveEvent(self, event: QgsMapMouseEvent) -> None:
        self.qvm.setCenter(event.mapPoint())
        
    def canvasReleaseEvent(self, event: QgsMapMouseEvent) -> None:
        self.qvm.setCenter(event.mapPoint())
        if self.target_layer.isEditable():
            if self.target_layer.selectedFeatureCount() == 1:
                for feature in self.target_layer.getSelectedFeatures():
                    release_point = QgsGeometry.fromPointXY(event.mapPoint())
                    release_point.transform(qgis._core.QgsCoordinateTransform(self.iface.mapCanvas().mapSettings().destinationCrs(),self.target_layer.crs(), qgis._core.QgsProject.instance()))
                    feature.setGeometry(release_point)
                    self.target_layer.updateFeature(feature)
                    self.iface.messageBar().pushMessage('MapToolUpdateNullGeometry', f'Geometry for feature #{feature.id()} created!', level=qgis._core.Qgis.Success)
            else:
                self.iface.messageBar().pushMessage('MapToolUpdateNullGeometry', 'Please select single feature!', level=qgis._core.Qgis.Info, duration=10)
        else:
            self.iface.messageBar().pushMessage('MapToolUpdateNullGeometry', 'Target-Layer not editable!', level=qgis._core.Qgis.Info, duration=10)
            

        

map_tool = MapToolUpdateNullGeometry(iface,iface.activeLayer())
iface.mapCanvas().setMapTool(map_tool)
_______________________________________________
QGIS-User mailing list
QGIS-User@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to