3

I have a layer of a csv file with around 700k rows and a polygon layer, both are in wgs84.

I'm trying with the spatial query to calculate what point are in the polygons.

The problem is that the spatial query doesn't count all the points when it's finished, as in the attached picture.

It should mark all points within the polygon.

enter image description here

whyzar
  • 12,053
  • 23
  • 38
  • 72
Tobbe
  • 611
  • 1
  • 5
  • 13
  • can you try the spatial query plugin from the processing toolbox? the plugin has been deprecated in favor of the processing tool. – SaultDon Dec 13 '17 at 03:40

2 Answers2

4

Try to use Select Within Plugin instead of Spatial Query. It is useful mainly within the selections between two polygon layers, but it should work to select points located within polygon layer.

Another advice is to save the CSV points as a shapefile. Try not to use CSV directly to select points located within polygon layer and use shapefile with a CRS of WGS 84 to keep it consistent with the CRS of the polygon layer. Sometimes working with CSV directly in QGIS can produce undesirable result.

ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
2

Agree that csv might cause problems. Another hint would be to check if your polygon layer has no topological inconsistencies (invalid geometries, i.e. duplicate consecutive points, self intersections, ...) You can check this under Vector > Topology Checker (under Plugins, active the Topology Checker Plugin). This shows you in the map where possible topo issues occur.

For further reading about repairing invalid geometries i.e. cp.

https://anitagraser.com/2017/08/29/fixing-invalid-polygon-geometries/

Fixing geometry validity errors in QGIS?

https://www.youtube.com/watch?v=BqJbVh_OqCE

It's not totally clear to me, whether you only want to select (or extract) the points WITHIN your polygon, or if you want to calculate statistics for the points within your polygons (counting them would be such statistics). In the first case you can use Select by location or Extract by location algorithms in the processing toolbox, in the latter case you can use algorithms like GRASS 7 v.vect.stats (cp. Populating polygon with most frequent point attribute using QGIS?) or SAGA point statistics for polygons (each found in the processing toolbox). I observed, that QGIS Join attributes by location algorithm produces weird results in some situations (cp. QGIS 'join attributes by location' algorithm produces strange results for min, max), but this might have been fixed meanwhile.

Here is a screenshot where to find the algorithms, in each you can select 'Save to file', where you can even choose csv format:

enter image description here

enter image description here

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
  • Hi. I want to make a selection of the points within the polygons and then save the selected ones to a csv file. – Tobbe Dec 13 '17 at 08:25
  • I think in this case Extract by location is the tool for you. This generates a new layer or can save directly to csv. But be aware to 1. do the topo check 2. convert your points to shp before your analysis. – Jochen Schwarze Dec 13 '17 at 08:29
  • I've used the extract by location now and i get the exact same problem. Also used the topology checker and no problems where found – Tobbe Dec 13 '17 at 10:47
  • Can you upload and provide your datasets somwhere to have a closer look at? – Jochen Schwarze Dec 13 '17 at 10:49
  • I ran the Topology checker again and it finds 273 invalid geometry. Is there a way to automatically correct these? – Tobbe Dec 13 '17 at 10:57
  • not impossible, me thinks, without having had a closer look at. See https://anitagraser.com/2017/08/29/fixing-invalid-polygon-geometries/ https://gis.stackexchange.com/questions/112687/fixing-geometry-validity-errors-in-qgis https://www.youtube.com/watch?v=BqJbVh_OqCE for reference, or g**** for sth like 'qgis repair invalid geometry' – Jochen Schwarze Dec 13 '17 at 11:34
  • updating my answer with these links. – Jochen Schwarze Dec 13 '17 at 11:35
  • That worked great!

    But the fix only does an output of the corrected polygons. The original layer contains much more. How do i now combine these and delete the old bad polygons and replace them with the new functioning ones?

    – Tobbe Dec 13 '17 at 13:38