Is it possible (without writing Python code - sorry I'm not a developer) to create a point layer from a polygon layer but ensure the point is within the polygon? The ftools centroid tool is cool but some centroids are created outside the polygon (i.e. irregular polygon). In ArcGIS there is a "Feature to Point" tool similar to the ftools 'polygon centroids' tool but has a checkbox to force the point to be within the polygon.
3 Answers
If you do not care about whether the point is a centroid or not, just use Vector -> ResearchTools -> Random Points. You may define to place a single point per each polygon in a layer. Also you may create centroids for initial polygons, delete points that do not intersect any polygon, save polygons without points as a separate layer, create random points for this polygosns and in the end copy these random points to centroids layer.
- 11,519
- 3
- 42
- 75
- 5,621
- 3
- 27
- 62
-
+1 Nice answer! never thought such a possibility existed.. – vinayan Aug 23 '12 at 14:38
-
It was a nice answer and fixed my problem. Thanks SS_Rebelious. For the benefits of others, I needed this function to address a spatial query. I have two layers of the same theme (Open Space) from 2 different organisations. They are very similar but do not match spatially (can't use a spatial Equals). I need a one-to-one match to compare features but a spatial intersect query selects adjacent feature due to the overlap(poor topology). Converting one layer to points enables me to do a Within spatial query, which is not perfect but a much closer fit. – Colin Aug 24 '12 at 04:04
EDIT : answer by @Kampau Ocu : "point on surface is already within QGIS3 Processing Toolbox"
You can also use the plugin RealCentroid that does just what you're looking for :
RealCentroids plugin creates a point shape file with internal points of a polygon shape, similar to PostGIS (GEOS) ST_PointOnSurface. The point will be inside the polygon in all cases. Not only the points are created but the attributes are also copied from the polygon to the internal points . A single point is generated for multipart geometry too.
- 7,818
- 2
- 29
- 73
I agree that the "point on surface" tool is a good way to get pseudo-centroids that are always within a polygon.
I am using QGIS 3.22 and you can find the tool in the Processing Toolbox (search for 'point on surface'). However, it's also a simple few lines of code to run in the QGIS Python console:
x = 'Parks' # name of your map layer
vLayer = QgsProject.instance().mapLayersByName(x)[0]
params = {'INPUT': vLayer,'ALL_PARTS':False,'OUTPUT':'TEMPORARY_OUTPUT'}
srfcPtRslt = processing.run("native:pointonsurface", params)
Here is a visual example of how 'Centroid' points (pink) differ from 'Point on surface' points (green):

- 1,043
- 7
- 18
