80 votes
Accepted

Understanding difference between Polygon and Multipolygon for shapefiles in QGIS?

From a more formal point of view, I report the definition for both a polygon and a multipolygon (the following text and images come from this document of the OGC® Standards, all credits to the cited ...
mgri's user avatar
  • 16.1k
37 votes

Extracting intersection areas in R

Here's an alternate approach using the new sf package, which is meant to replace sp. Everything is much cleaner, and pipe friendly: library(sf) library(tidyverse) # example data from raster package ...
Matt SM's user avatar
  • 1,831
35 votes
Accepted

Check if a point falls within a multipolygon with Python

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 ...
gene's user avatar
  • 54.4k
35 votes
Accepted

Get field names of shapefiles using GDAL

1) individual shapefile: as in the comment, a shapefile has only one layer. If you want only the names of the fields from osgeo import ogr source = ogr.Open("a_shapefile.shp") layer = source.GetLayer(...
gene's user avatar
  • 54.4k
34 votes

Is there an automatic function in QGIS to package all files in a project for transferability?

I have just tested the three plugins that attempt to do what you ask on a project file with a range of file based formats. Here are the results: Plugins tested were: QConsolidate 0.2.1 QPackage 1.3 ...
Andy Harfoot's user avatar
  • 3,297
32 votes

Renaming attributes/fields in shapefile attribute table using QGIS

Since QGIS 2.16 you no longer need a plugin to do this - you can double click a field name in a shapefile from the vector layer properties window and rename (set the layer as editable first)
ndawson's user avatar
  • 27.4k
31 votes

Understanding difference between Polygon and Multipolygon for shapefiles in QGIS?

Shapefile polygons are constructed from one or more closed rings. If a shape has multiple rings, they can be exterior and interior (e.g., form a doughnut), or multiple exterior (e.g., island chain). ...
Vince's user avatar
  • 19.9k
30 votes

GeoJSON too bulky

Mapshaper.org is a handy free online tool that allows you to upload a geojson file, display it as a map, then choose one of three simplification alogrithims which you can adjust the strength of with a ...
user56reinstatemonica8's user avatar
29 votes
Accepted

Import a shapefile to postgis with ogr2ogr gives: Unable to open datasource

As you discovered by trial and error, there were few nagging issues you needed to fix, the last of which was resolved using ogr2ogr's -nlt GEOMETRY* argument. * Note the recommendation in @...
elrobis's user avatar
  • 6,430
28 votes

Opening shapefile in R?

You can use sf library to open Shapefiles directly in R. It's faster than rgdal library, check here: Simple Features for R - Benchmarks. For further information about the sf package check the project ...
Guz's user avatar
  • 3,166
28 votes

Calculate area for each polygon in R

You can do library(raster) x <- shapefile('file.shp') crs(x) x$area_sqkm <- area(x) / 1000000 Or with terra library(terra) x <- vect('file.shp') x$area_sqkm <- expanse(x) / 1000000 That ...
Robert Hijmans's user avatar
26 votes
Accepted

Setting default value for attributes in shapefiles using QGIS

QGIS 3 Select the layer in the layer panel and left-click. Select properties. Select the "Attributes Form" as shown below. Select the fields from the "Available Widgets" list as show. Provide an ...
Matthias Kuhn's user avatar
26 votes
Accepted

Python can't read shapefile

There's a trap for first time users of shapefiles. The actual shapefile (.shp) is useless without the companion files: .dbf, .shx, .prj etc.. This is outlined in the shapefile technical specification ...
Michael Stimson's user avatar
26 votes

Make shapefile from raster-bounds in Python

With shapely box and GeoPandas import rasterio as rio ra = rio.open("raster.tif") bounds = ra.bounds Convert bounds to shapely geometry from shapely.geometry import box geom = box(*bounds) print(...
gene's user avatar
  • 54.4k
25 votes

Making shapefile from Pandas dataframe?

Yes, that can be done with shapely and geopandas. Supposed that your pandas dataframe kind of looks like this: import pandas as pd data = [ {'some_attribute': 'abc', 'lat': '50.1234', 'lon': '...
Dirk's user avatar
  • 1,819
25 votes
Accepted

Shapefile loader in pgAdmin 4

I also had to upload a shapefile to postgresql and found this solution perfect. Navigate to the given path "C:\Program Files\PostgreSQL\9.6\bin\postgisgui" Run the gui file "shp2pgsql-gui.exe"and a ...
Shahzad Bacha's user avatar
25 votes
Accepted

Dissolving not based on attribute in GeoPandas

You will lose all attributes but unary_union will return one big multipolygon which you can turn into singleparts with .geoms: import geopandas as gpd df = gpd.read_file(r"C:\Test\New_Shapefile.shp") ...
BERA's user avatar
  • 68.6k
24 votes
Accepted

ogr2ogr error importing shapefile into PostGIS : "numeric field overflow"

For making the conversion to success read the manual page http://www.gdal.org/drv_pg.html from section Layer Creation Options PRECISION: This may be "YES" to force new fields created on this layer ...
user30184's user avatar
  • 63k
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.4k
23 votes

Reading Large Shapefile in R - alternatives to readOGR?

The sf package does read much, much more quickly. Using the Natural Earth roads layer, with ~ 60k features: require(rgdal) require(sf) dsn <- "./Natural_Earth_v2.0.0/10m_cultural/10m_cultural" ...
Simbamangu's user avatar
  • 14.8k
22 votes
Accepted

Can colon (":") character be used in shapefile field names?

Field name restrictions inside a shapefile are determined by dBase limitations. There are four single rules: Up to 10 character max. First character in field name must start with a letter. Field ...
aldo_tapia's user avatar
  • 13.2k
22 votes
Accepted

Explaining difference between shx and shp files of shapefile?

The definitive reference on the shapefile format is the ESRI Shapefile Technical Description. It is misleading to describe the shx as being an "index." Instead, it is the direct access ...
Vince's user avatar
  • 19.9k
21 votes

Generating start and end points for LineStrings

If you have QGIS 3.4 you can use Extract specific vertices tool to extract the endpoints by inputting 0, -1 at the vertex indices as you can see below: The tool is located in Processing toolbox -> ...
ahmadhanb's user avatar
  • 40.4k
20 votes

Seeking Free Shapefile of European Countries?

You can download the "official" EU administrative/statistical units from Eurostat's GISCO service. The vector layers are available as ESRI Shapefiles and Personal GDBs, at different resolutions from ...
dmci's user avatar
  • 4,872
20 votes
Accepted

AttributeError "'int' object has no attribute 'encode'" when writing GeoDataFrame into shapefile or spatialite with GeoPandas

As I don't know your data, I give you a solution with my data (with x,y, z and a colum to test < 30). If I use your solution import geopandas as gpd import numpy as np import pandas as pd ...
gene's user avatar
  • 54.4k
19 votes
Accepted

Run SQL queries against shapefile in QGIS?

You can use a virtual layer. Load your Shapefile in QGIS, then go to Layer / Add Layer / Add-Edit Virtual Layer. Type the SQL you want. For instance, if your shapefile is named myshp and has ...
JGH's user avatar
  • 40.7k
18 votes

Exporting attributes in KML file using QGIS

In newer versions (I used QGIS 2.18.9) if you right-click on the layer -> "Save As..." you can toggle "Select fields to export and their export options". There you can select all the fields you want ...
Zeronull's user avatar
  • 336
18 votes
Accepted

Cleaning large Shapefile using v.clean in order to dissolve features?

The source data seems to be rather hard to handle as vectors as you have noticed. However, this workaroung that goes through an intermediate raster file works well and it is very fast. 1) Use ...
user30184's user avatar
  • 63k
18 votes

Opening PRJ file in text editor?

Right click and open with a text editor like notepad (or Notepad ++). It's not a GIS layer, just some projection metadata on your layer. A shapefile is made of severall files with some mandatory. The ...
gisnside's user avatar
  • 7,798
18 votes

Permission denied when creating shapefile

Before trying to save the file, click on the folder button at the right of the filename entery box and select a writeable folder for the save.
Bill C's user avatar
  • 957

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