1

Using PyQGIS, I desperately need help on creating a buffer around a point in a node layer and then selecting features from a trails layer that fall within the buffer. When I load the shape files without OpenLayers, it works. If I load a base map from OpenLayers like Bing Aerial or Google Satellite, the buffer no longer finds any features from the trails layer.

Also, zoom to location works great without OpenLayers, but when I bring in the map, it zooms in so far that all I see is a blank screen, and I have to manually zoom out quite a bit to see the shape files.

The base map is critical for the project or I would just do without.

Here is the code for selecting the trail features within the node buffer. If the feature we are looking for is not part of the selection, the code then zooms to the buffer bounding box around the node so the user can manually select a new trail. The base map is necessary in order to manually make a proper selection.

def check_trail(self, node_id):

    # Check if trail record has changed.

    found = 0
    canvas = self.iface.mapCanvas()

    request = QgsFeatureRequest().setFilterFid(node_id)
    feature = self.nodes_layer.getFeatures(request).next()

    pntGeom = feature.geometry()
    pntBuf = pntGeom.buffer(canvas.mapUnitsPerPixel()*30, 0)
    rect = pntBuf.boundingBox()

    # Convert the rectangle coordinates to the layer CRS.
    layerRect = canvas.mapRenderer().mapToLayerCoordinates(self.trailfile, rect)

    # save currently select trailfile
    trail_current = self.trailfile.selectedFeaturesIds()

    # remove current trailfile selection
    self.trailfile.removeSelection()

    # Select the features that are in the rectangle and return their fids.
    self.trailfile.select(layerRect, False)

    # get selected trails
    trails_found = self.trailfile.selectedFeaturesIds()

    for trail in trails_found:
        self.trailfile.removeSelection()
        self.trailfile.select(trail)
        temp_trail_name = self.trailfile.selectedFeatures()[0].attribute('name')

        # compare buffered trail name with the most recent trail name obtained
        if temp_trail_name == self.trails[-1]["name"]:
            # add trail
            found = -1
            break

    if not found:
        # Zoom to location where there is a new trail
        self.trailfile.removeSelection()
        selectList=[node_id]
        self.nodes_layer.setSelectedFeatures(selectList)
        box = self.nodes_layer.boundingBoxOfSelected()
        self.iface.mapCanvas().setExtent(box)
        self.iface.mapCanvas().refresh()
        self.trailfile.select(trail_current)

        # select trail to attach to segment
        while not self.selectFeatureResult and not self.cancelProdUnits:
            # Set the canvas click response to select a trail.
            self.clickResponse = select_trail
            self.selectTrail.show()
            self.selectTrail.exec_()
        self.selectFeatureResult = 0
1

Found that I could use the TileLayer plugin https://github.com/minorua/TileLayerPlugin instead of openlayers. After creating a .tsv file for Google image layers, I am able to load a google layer base map which zooms fine and has no problem with buffering. Unfortunately, if visible, it does slow down the buffering process considerably. I just turn it off when working processing.

1

You can also give a try with the QuickMapServices Plugin which is a nice tool to add Basemap in projects (OSM, Google, Bing,... and many others).

To enjoy the full power of this plugin, make sure to get Contributed Pack in QuickMapServices > Settings > More services > Get contributed pack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.