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]
...
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 ...
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
(...
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 ...
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 ...
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
...
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 ...
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
...
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 ...
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)....
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)
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 ...
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
# <...
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 ...
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 = ...
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....
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', '...
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 ...
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 ...
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 ...
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 ...
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, "...
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)),...
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 ...
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) ...
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....
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 ...
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);
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 ...
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
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
geometry × 1815qgis × 408
postgis × 280
python × 193
polygon × 186
arcpy × 164
arcgis-desktop × 119
postgresql × 119
shapefile × 92
pyqgis × 86
coordinate-system × 82
sql × 73
sql-server × 66
line × 57
geopandas × 57
intersection × 55
shapely × 51
arcmap × 49
qgis-3 × 48
point × 45
geojson × 42
linestring × 39
well-known-text × 39
vector × 38
ogr × 38