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 ...
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
...
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 ...
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(...
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
...
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)
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).
...
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 ...
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 @...
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 ...
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 ...
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 ...
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 ...
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(...
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': '...
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 ...
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")
...
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
...
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
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"
...
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 ...
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 ...
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 -> ...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shapefile × 5541qgis × 1648
arcgis-desktop × 652
python × 611
r × 419
coordinate-system × 353
polygon × 316
raster × 275
postgis × 253
arcmap × 252
gdal × 252
convert × 239
arcpy × 209
ogr2ogr × 199
data × 177
csv × 165
ogr × 163
geojson × 146
pyqgis × 145
geopandas × 141
attribute-table × 138
kml × 136
vector × 133
export × 126
fields-attributes × 122