47 votes

Filter by bounding box in GeoPandas

You can use the cx method on a geodataframe to select rows within a bounding box. For your example frames: xmin, ymin, xmax, ymax = df_sussex.total_bounds sac_sussex = df_sac.cx[xmin:xmax, ymin:ymax] ...
jdmcbr's user avatar
  • 904
28 votes

Filling holes in polygons automatically using QGIS

For QGIS 3 The "Delete Holes" tool combines both of the above functionalities. You can optionally specify a maximum area of hole to delete. If left as 0.0, the tool will delete all holes of ...
Joseph's user avatar
  • 75.3k
24 votes

In PostGIS: how to split linestrings into their individual segments?

You are going about it the right way, using ST_PointN and generate_series. One way of doing this, using dummy data (substitute your own in initial CTE) would be: WITH sample(geom, id) AS (...
John Powell's user avatar
  • 13.6k
24 votes
Accepted

Can a PostGIS table or view have two geometry columns?

You can have many geometry columns. You can name them whatever you want. There is no 'default' geometry. It's just another attribute. That would be like asking which is the default date for a feature ...
alphabetasoup's user avatar
23 votes
Accepted

Inverting Polygons using QGIS?

You can do this using the following steps: 1- In QGIS go to Vector > Research Tools > Polygon From Layer Extent. Use the World's Land area as Input File. Choose the Desired name for the output ...
ahmadhanb's user avatar
  • 40.5k
23 votes
Accepted

Removing small holes from the polygon

You can use the following example script. The script removes all holes from a polygon whose area is greater than eps. from shapely import wkt from shapely.geometry import Polygon # sample polygon ...
Kadir Şahbaz's user avatar
22 votes
Accepted

Difference between geometry() and $geometry in QGIS

Right next to the list of functions your screenshot is of, should be the currently selected function's documentation. For me, with QGIS 2.18.12 those read: $geometry Returns the geometry of the ...
bugmenot123's user avatar
  • 10.8k
21 votes
Accepted

Reading geopackage geometries in Python

You can use the gdal/ogr, fiona (built on gdal/ogr) or geopandas (built on fiona) python libraries. Below is a fiona example: import fiona # No need to pass "layer='etc'" if there's only one layer ...
user2856's user avatar
  • 64.9k
20 votes

Filling holes in polygons automatically using QGIS

I faced the same problem as yours with this shapefile, having many gaps between polygons. In order to solve the problem, You could use a slight modification of tshiffle's method: First, perform the ...
gtapko's user avatar
  • 831
20 votes
Accepted

Centroid coordinates for odd-shaped polygons

I think the standard QGIS polygon centroid tools calculate the centre of mass in order to determine the polygon's centroid. So this could lie outside the polygon itself (nothing to do with projection)....
Joseph's user avatar
  • 75.3k
20 votes
Accepted

Calculate distance from a Point within a Polygon to its edge

Compare the point to the polygon's exterior ring: poly.exterior.distance(point)
sgillies's user avatar
  • 8,996
20 votes

Creating square buffers around points using shapely?

Here is how you can create square buffers using geopandas. Note that geometric operations in geopandas are performed by shapely. import geopandas as gpd from shapely.geometry import Point import ...
Aaron's user avatar
  • 51.5k
19 votes
Accepted

Difference between QgsPoint, QgsPointXY and QgsGeometry.fromPointXY() in PyQGIS

QgsPoint is a point class which supports Z (3D) and M values. (What are Z and M?) x, y, z, m = 10, 10, 20, 5 QgsPoint(x, y) # 2D # <QgsPoint: Point (10 10)> QgsPoint(x, y, z) # 3D # <...
Kadir Şahbaz's user avatar
17 votes

Visualising WKT geometry string in QGIS

No Plugin required In QGIS deploy a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer.... In the Query window simply paste the following expression: SELECT ...
Taras's user avatar
  • 31.4k
17 votes
Accepted

Transforming single QgsGeometry object from one CRS to another using PyQGIS

Use QgsGeometry.transform(QgsCoordinateTransform). For example after created your instance of QgsCoordinateTransform with source and dest crs, for each geometry instance do: sourceCrs = ...
Luigi Pirelli's user avatar
17 votes
Accepted

Drawing ellipse with shapely

Answer First, define a circle of radius 1. Then define an ellipse along x and y axis. Rotate the ellipse. Finally, actually draw the ellipse. Code from matplotlib import pyplot from shapely.geometry....
swiss_knight's user avatar
  • 9,794
17 votes

Difference between geometry() and $geometry in QGIS

$geometry returns the geometry of the current feature as in: geom_to_wkt($geometry) geometry() returns the geometry of a specific feature as in geom_to_wkt(geometry(get_feature('my_layer', '...
mixedbredie's user avatar
  • 1,870
17 votes
Accepted

Deleting small holes in polygons specifying the size with PostGIS

This is one of those processes which are simple in an iterative language, but trickier in set-oriented SQL. But the ST_DumpX set-returning functions in PostGIS and the array handling in Postgres make ...
dr_jts's user avatar
  • 4,733
16 votes
Accepted

How exactly is the centroid of polygons calculated?

If QGIS is computing the centroid with GEOS which is a JTS port then the algorithm is this http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/algorithm/CentroidArea.html. About the theory ...
user30184's user avatar
  • 63.2k
16 votes
Accepted

How to snap a road network to a hexagonal grid in QGIS?

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 ...
Germán Carrillo's user avatar
16 votes
Accepted

Postgres/PostGIS calculate area of Polygon in Square Miles

ST_Area will use the units in your coordinate system, which as you say are degrees. So you have two options: transform your geometries to a coordinate system that uses feet or meters cast your ...
amball's user avatar
  • 1,542
16 votes

Identify polygon containing point with R sf package

What you are looking can be done using sf::st_intersects() as commented. I provide a full working example using USA states. library(magrittr) library(ggplot2) library(sf) tt <- read_sf(path, "...
dof1985's user avatar
  • 3,146
15 votes

Determining the centroid of polygons in QGIS

A more robust soulution to mike's answer: long = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*')) lat = toreal(regexp_substr(geom_to_wkt(centroid($geometry)),...
SZIEBERTH Ádám's user avatar
15 votes
Accepted

Difference between bounding box, envelope, extent, bounds?

I think you'll find there is a bit of overlap with these definitions. They're all very similar, in my opinion. However, ESRI has a glossary of GIS terms, so I just looked them up. The definitions are ...
Fezter's user avatar
  • 21.8k
15 votes
Accepted

QGIS: "Invalid geometry" even after using validity checker and v.clean

I ran the Check validity from the dropdown Used the default settings The result was an Invalid Output I then copied and pasted the co-ordinated into the QGIS project window screen (Centre bottom) ...
Martin Hügi's user avatar
  • 3,602
15 votes
Accepted

TypeError: Input geometry column must contain valid geometry objects

I tested the following and works fine: from shapely import wkt import pandas as pd df = pd.DataFrame( {'name': ['testname', 'testname'], 'geom': ['MULTIPOLYGON (((21.78 40.36, ... 21.77 40....
pan's user avatar
  • 537
14 votes

Multipoint vs point PostGIS

The easiest way to extract a point from a single-point MULTIPOINT is ST_GeometryN: SELECT ST_AsText(ST_GeometryN('MULTIPOINT ((1 1))', 1)); --POINT(1 1) This avoids potential problems in ...
dbaston's user avatar
  • 13k
14 votes

converting/parsing geojson into com.vividsolutions.jts geometry

For a much more lightweight alternative to GeoTools, check out jts2geojson: GeoJSONReader reader = new GeoJSONReader(); Geometry geometry = reader.read(json);
dbaston's user avatar
  • 13k
14 votes

Reading geopackage geometries in Python

Geopandas is extremely useful and easy to use for this kind of thing: http://geopandas.org/ You can load vector data with attributes (e.g. from geopackages, shapefiles, etc.) into ?(geo)pandas ...
sbphd's user avatar
  • 216
14 votes

Creating centroid inside of polygon

If you want a point that is for sure inside the polygon use representative point: Returns a cheaply computed point that is guaranteed to be within the geometric object. import geopandas as gpd ...
BERA's user avatar
  • 68.8k

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