I'm trying to convert a polygon shapefile of the UK into a hex grid. Using MMQGIS I've produced the grid, then I tried the intersect tool which gave me something close to what I want. However, the irregular edge of the UK borders has cut some of the hexagons. Is there a way of keeping all of the hexagons whole and producing an 'approximation' of the original polygon.
[GIS] How to convert polygon into hexagonal grid
hexagon-binninghexagonal gridmmqgisqgis
Related Solutions
My solution involves a PyQGIS script that is faster and more effective than a workflow involving snapping (I gave it a try too). Using my algorithm I've obtained these results:
You can run the following code snippets in sequence from within QGIS (in the QGIS Python console). At the end you get a memory layer with the snapped routes loaded into QGIS.
The only prerequisite is to create a multipart road Shapefile (use Processing->Singleparts to multipart
, I used the field fictitiuos
as Unique ID field
parameter). This will give us a roads_multipart.shp
file with a single feature.
Here is the algorithm explained:
Get the nearest hexagon sides where routes cross. For each hexagon we create 6 triangles between each pair of neighbour vertices and the corresponding centroid. If any road intersects a triangle, the segment shared by the hexagon and the triangle is added to the final snapped route. This is the heavier part of the whole algorithm, it takes 35 seconds running on my machine. In the first two lines there are 2 Shapefile paths, you should adjust them to fit your own file paths.
hexgrid = QgsVectorLayer("/docs/borrar/hex_grid_question/layers/normal-hexgrid.shp", "hexgrid", "ogr") roads = QgsVectorLayer("/docs/borrar/hex_grid_question/layers/roads_multipart.shp", "roads", "ogr") # Must be multipart! roadFeat = roads.getFeatures().next() # We just have 1 geometry road = roadFeat.geometry() indicesHexSides = ((0,1), (1,2), (2,3), (3,4), (4,5), (5,0)) epsilon = 0.01 # Function to compare whether 2 segments are equal (even if inverted) def isSegmentAlreadySaved(v1, v2): for segment in listSegments: p1 = QgsPoint(segment[0][0], segment[0][1]) p2 = QgsPoint(segment[1][0], segment[1][1]) if v1.compare(p1, epsilon) and v2.compare(p2, epsilon) \ or v1.compare(p2, epsilon) and v2.compare(p1, epsilon): return True return False # Let's find the nearest sides of hexagons where routes cross listSegments = [] for hexFeat in hexgrid.getFeatures(): hex = hexFeat.geometry() if hex.intersects( road ): for side in indicesHexSides: triangle = QgsGeometry.fromPolyline([hex.centroid().asPoint(), hex.vertexAt(side[0]), hex.vertexAt(side[1])]) if triangle.intersects( road ): # Only append new lines, we don't want duplicates!!! if not isSegmentAlreadySaved(hex.vertexAt(side[0]), hex.vertexAt(side[1])): listSegments.append( [[hex.vertexAt(side[0]).x(), hex.vertexAt(side[0]).y()], [hex.vertexAt(side[1]).x(),hex.vertexAt(side[1]).y()]] )
Get rid of disconnected (or 'open') segments by using Python lists, tuples, and dictionaries. At this point, there are some disconnected segments left, i.e., segments that have one vertex disconnected but the other one connected to at least other 2 segments (see red segments in the next figure). We need to get rid of them.
# Let's remove disconnected/open segments lstVertices = [tuple(point) for segment in listSegments for point in segment] dictConnectionsPerVertex = dict((tuple(x),lstVertices.count(x)-1) for x in set(lstVertices)) # A vertex is not connected and the other one is connected to 2 segments def segmentIsOpen(segment): return dictConnectionsPerVertex[tuple(segment[0])] == 0 and dictConnectionsPerVertex[tuple(segment[1])] >= 2 \ or dictConnectionsPerVertex[tuple(segment[1])] == 0 and dictConnectionsPerVertex[tuple(segment[0])] >= 2 # Remove open segments segmentsToDelete = [segment for segment in listSegments if segmentIsOpen(segment)] for toBeDeleted in segmentsToDelete: listSegments.remove( toBeDeleted )
Now we can create a vector layer from the list of coordinates and load it to the QGIS map:
# Create a memory layer and load it to QGIS map canvas vl = QgsVectorLayer("LineString", "Snapped Routes", "memory") pr = vl.dataProvider() features = [] for segment in listSegments: fet = QgsFeature() fet.setGeometry( QgsGeometry.fromPolyline( [QgsPoint(segment[0][0], segment[0][1]), QgsPoint(segment[1][0], segment[1][1])] ) ) features.append(fet) pr.addFeatures( features ) vl.updateExtents() QgsMapLayerRegistry.instance().addMapLayer(vl)
Another part of the result:
Should you need attributes in the snapped routes, we could use a Spatial Index to rapidly evaluate intersections (such as in https://gis.stackexchange.com/a/130440/4972 ), but that's another story.
Hope this helps!
Best Answer
You actually want a
selection
of the grid rather than a geometric modification of it so useSelect by Location
(one menu lower down underVector Selection Tools
) and chose intersects and contains to pick up all the hexagons that are within and overlapped by the UK polygons. Then you'll need to save the selection to a new file of course.