# Tag Info

38

Here is an example using a SpatialGrid object: ### read shapefile library("rgdal") shp <- readOGR("nybb_13a", "nybb") proj4string(shp) # units us-ft #  "+proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 # +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83 # +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0" ### define ...

32

over() from package sp can be a little confusing but works well. I'm assuming you've already made "A" spatial with coordinates(A) <- ~longitude+latitude: # Overlay points and extract just the code column: a.data <- over(A, B[,"code"]) Instead of a point spatial object, this simply gives you a data frame, with the same no. rows as A, and a single ...

29

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one feature = list of polygons, look at Converting huge multipolygon to polygons) The problem If I open a MultiPolygon shapefile, the geometry is 'Polygon' multipolys = fiona.open("multipol.shp") multipolys.schema {'geometry': 'Polygon', '...

21

If poly is a GeoDataFrame with a single geometry, extract this: polygon = poly.geometry Then, you can use the within method to check which points are within the polygon: points.within(polygon) returning a boolean True/False values which can be used to filter the original dataframe: subset = points[points.within(polygon)]

19

Convex hull - as mentioned by Kazuhito - is one option, but - depending on the cluster shape - you will get more appropriate polygons using concave hulls, for example implemented in ConcaveHull plugin.

18

The tool you're looking for is now called Count points in polygons, and it can be found in Processing Toolbox -> QGIS Geoalgorithms -> Vector analysis tools.

13

\$sql = "SELECT points.name FROM polygons, points WHERE ST_CONTAINS(polygons.geom, Point(points.longitude, points.latitude)) AND polygons.name = 'California'";

12

The New York dataset provided in the question is no longer available for download. I use the nc dataset from sf package to demonstrate a solution using sf package: library(sf) library(ggplot2) # read nc polygon data and transform to UTM nc <- st_read(system.file('shape/nc.shp', package = 'sf')) %>% st_transform(32617) # random sample of 5 points ...

12

The best tool for this job in my experience is Add polygon attributes to points in the Processing toolbox. If it does not work directly with the CSV, just save the points to a Shapefile before you run the spatial join.

12

If we examine your polygon: polygon = shapefile_record['geometry'] print polygon.bounds (77.84476181915733, 30.711096140487314, 78.59476181915738, 31.28199614048725) From Shapely manual, object.bounds: Returns a (minx, miny, maxx, maxy) tuple (float values) that bounds the object. Here minx = 77.84476181915733, miny = 30.711096140487314 = here, min ...

12

I used this code: mapcanvas = iface.mapCanvas() layers = mapcanvas.layers() processing.runalg('qgis:selectbylocation', layers, layers, u'within', 0) with this situation: and it worked: Updating for QGIS 3: Following code works in QGIS 3.20 (Odense): mapcanvas = iface.mapCanvas() layers = mapcanvas.layers() parameters = { 'INPUT' : layers, ...

12

Why obstacles do not work The setting Features act as obstacles only works for features of the same layer. Use Geometry generator to define label placement Go to Label / Placement tab and use Geometry generator to define the area inside the polygon where the label is allowed to be placed. Use an expression that excludes the area around the points: create a ...

11

In QGIS, many of the really good tools are in the processing toolbox; you need 'concave hull': Try it with different threshold values for different levels of detail: Finally, add a 10% buffer around the outside to make it resemble the sketch you provided:

10

Try Concave hull, What are Definition, Algorithms and Practical Solutions for Concave Hull? Concave hull has a smaller area, and most of implementations allows you to tune how small and precise resulting polygon should be.

10

You should use the ST_within function and not the ST_intersect function. Here the documentation. Here the sql code. You want only the points within greatermanchester select a.* from "Nov01" as a join greatermanchester as b on ST_WITHIN(a.the_geom, b.geom)

10

You may be interested in Convex Hull which is in Processing | QGIS geoalgorithms | Vector geometry tools. There is Field option which can be used with Method Create convex hulls based on field. Or from the menu Vector | Geoprocessing Tools | Convex Hull(s). Many Thanks, Techie_Gus and underdark for information.

10

This expression will count the number of populated places for each country: aggregate(layer:='ne_110m_populated_places', aggregate:='count', expression:=name, filter:=contains(geometry(@parent), \$geometry) ) you can use the same expression to get the sum, just change the aggregate to 'sum' and the expression to the ...

10

Use this script: from PyQt5.QtCore import QVariant # layers polygon_lyr = QgsProject.instance().mapLayersByName("Polygons") point_lyr = QgsProject.instance().mapLayersByName("Points") # index number of "class" field field_index = point_lyr.fields().indexFromName('class') # unique class names unique_classes = point_lyr.uniqueValues(field_index) ...

10

You can use refFunctions Plugin. It adds custom user functions to QGIS Field Calculator. Then, you can add a new field to the point layer with the id of the polygon that contains it using the following expression in Field Calculator for the point layer: geomwithin('polygon_layer_name', 'id_field_of_polygon_layer')

10

There is also a possibility without need to install a plugin, simply using this expression. overlay_within() is available since QGIS 3.16, in fact implementing the functions of the refFunctions plugin in QGIS natively: https://qgis.org/en/site/forusers/visualchangelog316/#port-reffunctions-to-core array_to_string(overlay_within('polygon', id)) The first ...

10

You can use QGIS expressions with the new overlay_nearest function, availble since QGIS 3.16 and the array_mean() function, available since QGIS 3.18. If you already have an attribute in the polygon layer (lets say with fieldname values), than applying this epxression on the polygon layer with field calculator will get you the mean value of the neighboring ...

9

You first need to understand the different ways to create geometries and the geometrical relations in GeoDjango (GeoDjango: GEOS API), without a database: 1) create valid geometries: # with Point, Polygon objects of GeoDjango from django.contrib.gis.geos import Point, Polygon, poly = Polygon(((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)))...

9

The point.in.poly function in the spatialEco package returns a SpatialPointsDataFrame object of the points that intersect an sp polygon object and optionally adds the polygon attributes. First lets add the require packages and create some example data. require(spatialEco) require(sp) data(meuse) coordinates(meuse) = ~x+y sr1=Polygons(list(Polygon(cbind(c(...

9

The problem in your first example is in this loop: ... shpfilePoints = [] for shape in polygon: shpfilePoints = shape.points ... It only appends the last feature points. I tried out my approach with this shapefile: I modified your code to: from shapely.geometry import Polygon, Point, MultiPolygon import shapefile path = '/home/zeito/pyqgis_data/...

9

There is a direct and very easy way to do this. In the Clip tool, press the green cyclic arrows button next to the Overlay layer dropdown menu. That instructs to iterate the operation over the features of the layer. You'll get seperate layers for each polygon. For more details check the "Iterative execution of algorithms" article from the QGIS ...

8

You can do this in two steps using QGIS. (1) First, use the tool the "Join Attributes by Location" to create a new field in your points layer that describes which polygon each point falls into: (2) Next, run the "Split Vector Layer" tool to write separate shapefiles for each unique value in the new field created by the previous step: ...

7

Three algorithms using different methods. Github Repo: https://github.com/imran-5/Postgis-Custom The simple and best approach, using the actual earth distance of coordinates from the x and y direction. The algorithm works with any SRID, internally it works with WGS 1984(EPSG:4326), and the result transforms back to input SRID. Function =====================...

7

Here's a way in R: Make a test raster, 20x30 cells, make 1/10 of the cells set to 1, plot: > require(raster) > m = raster(nrow=20, ncol=30) > m[] = as.numeric(runif(20*30)>.9) > plot(m) For an existing raster in a file, for example a geoTIFF, you can just do: > m = raster("mydata.tif") Now get a matrix of the xy coordinates of the 1 ...

7

Your guess is correct - use 'Count Unique Points in Polygon' in the processing toolbox, using "car_id" as the "Class field".

7

Spatial Join your points to your polygons, use INTERSECT or WITHIN, no need to keep all the attributes just the OID of the polygon is what's needed on the joined points. Using summary statistics you can count the points.. use a summary field of FID or OBJECTID depending on what sort of data you have (shape or GDB), summary type of count and a case field of ...

Only top voted, non community-wiki answers of a minimum length are eligible