New answers tagged

3

import geopandas as gpd import matplotlib.pyplot as plt gdf = gpd.read_file("path/to/shapefile.shp") for row in gdf.iterrows(): geom = gpd.GeoSeries(row[1].geometry) geom.plot()


1

import geopandas as gpd import matplotlib.pyplot as plt gdf = gpd.read_file("path/to/shapefile.shp") for row in gdf.iterrows(): plt.figure(figsize=(5,5)) plt.axis('equal') x, y = row[1].geometry.exterior.xy plt.plot(x, y) This script plots polygon's boundary. Shapefile: Separate Plots:


2

You can defined a plotting function and then call it on your GeoDataFrame. Let's dive into an example. Here are 4 polygons in a Shapefile (drawn with ♥ in QGIS): First do some imports: import geopandas as gpd import matplotlib.pyplot as plt # Load shapefile with geopandas poly_gdf = gpd.read_file('polygons.shp') Then, define a plotting function: def ...


1

Try to think the opposite way! It's basically a graph problem. Pseudo-code: Get the centroids for each cells. They are nodes. Draw the paths. Count the intersections between each path an cell's borders for each paths (subtract 1). They will eventually sum up if you need to take more than one edge to go from two nodes. Search for 'dual graph' and you will ...


1

A completely different solution with yet another resulting polygon shape consists of "stretching" the polygons: just extend the length (longer side) of the rectangle until its intersection with the lines layer. This is more intuitive, but not necessarily easier to realize. You can do it as follows. Remark: your polygon should have an uniqe ...


2

You polygons must have a unique identifier in the attributes, in my case the field named fid. I also presume the line layer has the name line. If you have other names, change it in the following expressions accordingly. Be aware: the expression overlay_nearest is available since QGIS 3.16, see visual changelog. For older versions, use the refFunctions plugin ...


0

When one creates a join and links an existing shapefile attribute table with another table, do export the two joined table as a new shapefile and load it again into QGIS. The joined table's fields will then be accessible to various QGIS tools, e.g., dissolve.


2

There are at least two possible solutions: 1. Use Geoprocessing / Difference A simple approach is to create a buffer around layer B - set the buffer distance high enough that all white spaces between the two layers are covered. See screenshot: Layer A: orange; Layer B: blue; buffered Layer B: red outline. Now use Menu Vector / Geoprocessing Tools / ...


1

To get a polygon layer that covers all - and only those - areas where you have (visible) pixels, proceed as follows, you can first create a binary raster with raster calculator, based on the values of the visible pixels, and than create contour polygons from it. Inspect your raster with Identify Features from the toolbar. In the screenshot below, I have a ...


2

You can create a new polygon by snapping each vertex of your original polygon to the nearest anchor point of the grid. For that, you can use the new overlay_nearest expression, available since QGIS 3.16. So each vertex of your polygon is shifte to the nearest anchor point of the grid. Create a point layer anchor (blue dots on the screenshot) representing ...


3

As @bugmenot123 stated, no need to use shapefile module and Fiona has all you need. import rasterio import fiona import numpy as np from openpyxl import load_workbook from rasterio.mask import mask wb_output = "path/to/excelfiel.xlsx" multispec = "path/to/image.tif" shp_file = "path/to/shapefile.shp" plot_num_field_name = "...


0

You could use Shapely in Python to identify the exterior and interior rings. https://shapely.readthedocs.io/en/stable/manual.html#polygons


2

Shapefiles don't distinguish between polygons and multi polygons, but many other programs do so it makes sense to "promote" polygons to multi when exporting them.


1

I inspected your data, the solution is based on that. First, create a new field longest that evaluates which side of your rectangle is the longest, width or height. Use this expression in the field calculator: if ( "width" > "height" , 'width', 'height'). Than you can create the centerline using this expression with Geometry generator ...


1

Your multiline is very difficult to polygonize. To show this I will use Shapely's polygonize_full operation which merges MultiLineString or list of LineString into polygons and returns four geometry collections: polygons, dangles, cuts and invalid ring lines The MultiLineString is composed of 4 LineString that intersects several times as in your example ...


1

Deniz is right, the problem is "difference" for field name because difference is: a) a method object of Shapely used by GeoPandas (object.difference(other) b) an index object of Pandas used by GeoPandas (pandas.Index.difference) import geopandas as gpd import matplotlib.pyplot as plt import descartes gdf = gpd.read_file("circles.shp") # ...


4

First an algorithmic description how this solution works. Afterwards the implementation in QGIS, using expressions to create new geometries. Description You can create for each vertex of your polygons the closest point on the line representing the street: make a perpendicular line on the streetline going through each vertex and where this connecting line ...


1

I suggest to use the "Oriented minimum bounding box" tool from the processing toolbox. "This algorithm calculates the minimum area rotated rectangle which covers each feature in an input layer." The output also contain the width, height, angle, perimeter and area of the bonding box. For mostly straight elongated polygon the width of the ...


5

You can use select by expression to select features on the same layer using the expression get_feature. The expression in this case is (see screenshot where the labels show the valule of the Seeding_Ra attribute for each polygon): overlaps( geometry ( get_feature ( @layer, 'Seeding_Ra', 33500 ) ), ...


0

For practical purposes, the maximum area possible should be calculated and it is possible to find that area without knowing any inside angle.


2

So to create simple polygons from a complex polygon (self-intersecting), is to use turf.unkink() for Javascript turf.js. The area can also be found using the polygon area equation. Example code below. Firstly, join the end point of the first curve with end of second curve, then join the start point of the second curve with start point of the first point. ...


1

Use Menu Vector / Geometry tools / Multipart to singleparts, see the documentation: https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectorgeometry.html#multipart-to-singleparts Your polygons are multipart, thus the feature consists not of one, but of several parts, thus multipart. What you want is to get one part (one geometry) per ...


2

The quickest way without opening the python console would be to do the following: run 'split vector layer' which creates a separate file for each of your vector features. Make sure to select an output directory. Then open all the vector files in qgis. now run 'clip raster by mask layer' and click on 'Run as Batch Process...' button in the bottom left hand ...


2

As an alternative, you can get the biggest polygons using exclusively QGIS expressions. Similar to @JGH's proposal, but you don't have to create a virtual layer, but it will still adapt dynamically to changing geometries if using geometry generator. In this case, you need no additional layer. This is if you need it for to visualize or for styling only. ...


3

Here's another alternative using a virtual layer Go to Layer > Add Layer > Add/Edit Virtual Layer... and enter the following prompt: WITH tbla AS (SELECT id, st_area(geometry) area, geometry FROM "Single parts") SELECT id, MAX(area) max_area, geometry FROM tbla I hope that your result meets your expectations...


4

You can achieve this in a single step using a virtual layer. The idea is to call QGIS function to order the multi part, as suggested by @Babel, and to select the 1st (biggest) one. The difference between the two approaches is how the biggest part is retained, and that a virtual layer is dynamic, i.e. if you modify the source shapes, it will be automatically ...


5

Let's assume there is a multi-polygon layer called 'polygons' with its attribute table, see image below Step 1. Apply "Multipart to singleparts" geoalgorithm Step 2. Use a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... with the following query: SELECT * FROM "Single parts" GROUP BY "id" ...


1

If you just want to calculate the distance from each buidling the the outline of the plots, go to the bottom to see how to calculate a new attribute field for that. Here, I first give some more detailed information and also a visualization of the line you're looking for. Let's supposte you have three layers named: buildings (polygon), plots (polygon) and ...


2

In the @Jose 's answer, instead of intersect we should use contains and in when condition we should use Boolean validation SELECT CASE WHEN (ST_Contains(the_geom, ST_Centroid(the_geom))) IS TRUE THEN ST_Centroid(the_geom) ELSE ST_PointOnSurface(the_geom) END AS center FROM someTable


1

So, your raw data in text format is shown below: P1 MULTIPOLYGON((( 20.0857554080356 49.1905790194619, 20.0887585780746 49.1908207752517, 20.0913701395045 49.1907900830797, 20.0919317359928 49.1903683481409, 20.0914538692795 **49.189904400853**, 20.0901285682587 **49.189850570172**, 20.0894706448357 49.1896357656678, 20.0895286338877 49.1890226017499, ...


3

I can't say how to correct the topology automatically but these images should show what happens. These are the vertices of the left side polygon (clipped1) and the location of the overlap. These are the vertices of the right side polygon (clipped2) and the location of the overlap. The overlap is in the middle of the segment of the right side polygon. The ...


0

To convert multipolygons to polygons with ST_Dump CREATE TABLE polytable AS SELECT id, (ST_Dump(w.geometry)).geom::geometry(Polygon,25833) AS geom FROM table_multi w


3

The methods mentioned by @Michael Stimson work very well. I tried both ArcGIS and QGIS. The "feature to polygon" in ArcGIS and "Polygon self-intersection" in QGIS do the same thing. They "return a new layer with sub-divided polygons (while only one of the duplicated overlapping area is retained)". The only difference is that in ...


2

You have to follow a couple of steps to achieve your spatial data file imported into database. Geodjango provides an effective tool, LayerMapping utility for converting geometry and field attributes from spatial formats like shapefile. Steps to follow: Decide where to import and process your file, admin or other views. Write a view and form accepting post ...


4

Since QGIS version 3.16, you have new overlay expressions you can use for this. I have a polygon layer with buildings (orange) and a street layer (blue), both from OpenStreetMap. You can create the shortest line from each building to the street-layer, thus to the nearest street feature, using this expression: shortest_line ( collect_geometries ( ...


6

To propose another method that does not imply any coding: you can create a model. That's probably easier to understand if you are not used to Python. You can however export the Model created in this way as a Python script. Go to Menu Processing / Graphical Modeler… to create a new model. From the input tab, select Vector features and give it a name, let's ...


5

By default I don't know, must be at least a little more complicated if possible I guess. But you can run "Add geometry attributes" as a batch process and apply it on all your files. Start it from processing toolbox and click on "Run as batch": Here you can then add your files / layers. Depending on your QGIS Version very convenient by ...


5

You can use pyqgis. Script below will add and calculate a field named area. If you have many layers it is possible to modify the code and process all at once. You will have to rerun the area calculation if you edit/modify the polygons, so it is not fully automatic. lyr = QgsProject.instance().mapLayersByName('Delaunay triangulation')[0] #Change to match your ...


1

Follow these steps: Now, carefully by using the Microsoft Excel add 2 field with names of e_end (or as you like for the y axis coordinate) and y_end as well The trick is copy your points coordinate without the first point coordinate and paste it in the first point e_end field and y_end field. "This means that the first point will be start point for ...


2

Another method - fit a smoothed line based on polar coordinates of the points from the centroid. Ingredients: a function to convert x,y to r,theta from the centroid of a set of points: polar <- function(pts,centre){ pts = st_coordinates(pts) theta = atan2(pts[,2]-centre[,2], pts[,1]-centre[,1]) r = sqrt((pts[,1]-centre[,1])^2 + (pts[,2]-centre[...


6

You can use DB Manager standard plug-in and SQL query on your project layers (even if these are not stored in an RDBMS). I suppose your layers have an id column which is a unique identifier for the features. Open the two layers (I draw something similar to your layers) using new and old for layer names Enable DB Manager plug-in Plugins/Manage and Install ...


4

Here's another method which orders the points by angle to get a first approximation to the "circumference" polygon. Use this function to get a polygon from the points, connecting points in increasing angle from the centroid (mean location) of the points: centrit <- function(pts){ centre = st_coordinates(st_centroid(st_union(pts))) pts = ...


1

Here's some sample points I created: > plot(pts) Now using a carefully selected buffer size wb (not too big, not too small) I can create a single thinnish polygon that contains the points: > wb = 0.015 > buf = st_buffer(st_union(pts), wb) > plot(buf,add=TRUE) Experiment with wb sizes to see the effect. That polygon is an outer ring and an inner ...


4

You can directly use the GDAL contour polygons tool. It is available from GDAL version 2.4.0. To see which GDAL version your QGIS installation uses go to Menu Help / About, see screenshot. If installed, it looks like this: If you have GDAL >= 2.4.0, but the entry contour polygons does not appear in the QGIS processing toolbox, you can use the "...


3

I tested the expression you provided with a random points layer and added four fields for "N", "E", "S" and "W" with random values. I just copied your expression without any minor change to my QGIS (point layer, add symbol layer, rendering type: geometry generator, geometry type: polygon) and everything works fine, see ...


1

The problem here that needs solving is due to the fact that the rows of buildings are different distances away from the park boundary. For example the buildings to the south have a road in-between them and the park. So the distances are not constant. Also the number of buildings per side varies. Finally the configuration of buildings varies. In the north you ...


1

You can use the select by location tool to select all points that fall inside the polygon. Set the points as input, select are within and the polygon as By comparing to the features from (see screenshot). You can than right click on the point layer / export / Save Selected Features As… and choose MS Office Open XML Table as output format to save only the ...


0

In addition to the indices you mention and the excellent skeletonization idea, there are a few more indices that could be interesting if you can calculate buffers and convex hulls easily: Fullness index: ST_Area(ST_Buffer(geometry, 0.177245385 * sqrt(ST_Area(geometry)))) / ST_Area(geometry) Depth index: ST_Area(ST_Buffer(geometry, -0.177245385 * sqrt(...


Top 50 recent answers are included