You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PyQGIS中输入点坐标?编写脚本实现用户输入坐标添加至Shapefile

QGIS Python Script: Add User-Defined Point to Existing Shapefile

Hey there! Let's get this script sorted out for you—here's the complete working version plus clear explanations of the key functions you'll need:

Complete Working Script

# Load the existing Shapefile
vectorLyr = QgsVectorLayer("D:/Projekty/qgis_projekt/forty922.shp", "Forty", "ogr")

# Validate if the layer loaded successfully
if not vectorLyr.isValid():
    print("Failed to load the Shapefile layer!")
else:
    # Get the layer's data provider (handles editing operations)
    vpr = vectorLyr.dataProvider()

    # Prompt user for X coordinate (returns value + confirmation boolean)
    x, ok_x = QInputDialog.getDouble(None, 'Input X Coordinate', 'Insert x: ')
    # Prompt user for Y coordinate
    y, ok_y = QInputDialog.getDouble(None, 'Input Y Coordinate', 'Insert y: ')

    # Proceed only if user clicked "OK" for both inputs
    if ok_x and ok_y:
        # Create point geometry (use QgsPoint instead of QgsPointXY for QGIS 2.x)
        pnt = QgsGeometry.fromPointXY(QgsPointXY(x, y))
        # Create a new empty feature
        f = QgsFeature()
        # Assign the point geometry to the feature
        f.setGeometry(pnt)
        # Add the feature to the layer via the data provider
        vpr.addFeatures([f])
        # Save changes to the Shapefile
        vectorLyr.commitChanges()
        # Refresh the layer to show the new point in QGIS
        vectorLyr.triggerRepaint()
        print("Point added successfully to the Shapefile!")
    else:
        print("User canceled the input operation.")

Key Functions Explained

  • QgsVectorLayer(): Loads your Shapefile—takes the file path, a friendly layer name, and the data driver (ogr is standard for vector files like Shapefiles).
  • dataProvider(): Retrieves the layer's data provider, which is required for any edit operations (adding/removing/modifying features).
  • QInputDialog.getDouble(): Pops up a dialog to get numeric input from the user. It returns two values: the input number, and a boolean (ok_x/ok_y) that tells you if the user clicked "OK" or canceled.
  • QgsGeometry.fromPointXY(): Creates a point geometry from your X/Y coordinates. Note: If you're using QGIS 2.x, replace this with QgsGeometry.fromPoint(QgsPoint(x,y)).
  • QgsFeature(): Creates an empty feature object that you can assign geometry and attributes to.
  • setGeometry(): Attaches the point geometry to your new feature.
  • addFeatures(): Adds the new feature to the layer via the data provider (needs to be passed as a list of features).
  • commitChanges(): Saves all your edits to the actual Shapefile file—don't skip this step, otherwise changes won't persist!
  • triggerRepaint(): Refreshes the layer in the QGIS interface so the new point shows up immediately.

内容的提问来源于stack exchange,提问作者Hubert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:37:11