Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Map Tools
Maps
postgis
Python
QGIS
Uncategorized

Converting CSV Files into Shapefiles with QGIS

Effortlessly Convert CSV Data to Shapefiles Using QGIS and Python

Are you working with geospatial data and need a quick and efficient way to convert your CSV files into shapefiles? Look no further! In this blog post, we’ll explore how you can leverage the power of QGIS and Python to transform your data into a more usable format for GIS analysis and visualization.

We’ll start by guiding you through the process of converting CSV data containing point coordinates into shapefiles using QGIS. Then, we’ll dive into more complex scenarios, such as converting CSV files with line or polygon data, and discuss how Python can be employed to automate these tasks.

Want to say ahead of the GIS curve? Listen to our podcast!

A step-by-step guide to how to convert a CSV file into a shapefile in QIGS

Step 1: Prepare the CSV file

  • Ensure that the CSV file contains geographic points with real-world coordinate values, such as latitude and longitude or UTM meters.
  • If the data is in another file format (e.g., XLS or XLSX), save it as a CSV file.

Step 2: Open QGIS

  • Launch the QGIS application.

Step 3: Import the CSV file

  • Click on the ‘Open Data Source Manager’ icon.
  • Select ‘Delimited Text’ at the top of the window.
  • Click ‘Browse’ and locate the CSV file on your computer, then click ‘Open’.

Step 4: Configure the layer settings

  • Give the output layer a name.
  • Ensure ‘CSV’ is selected as the file format.
  • Set the Coordinate Reference System (CRS) by clicking ‘Select CRS’ and choosing the appropriate CRS.

Step 5: Add the layer to the map

  • Click ‘Add’ to add the new layer to the map canvas, and then close the Data Source Manager.

Step 6: Export the layer as a shapefile

  • Right-click on the newly created layer in the Layers panel.
  • Choose ‘Export’ and then ‘Save Features As’.
  • In the ‘Format’ dropdown menu, select ‘ESRI Shapefile’.
  • Specify a file name and location for the new shapefile.

Step 7: Set the coordinate reference system for the shapefile

  • Ensure the correct CRS is selected. If not, click ‘Select CRS’ and choose the appropriate CRS.

Step 8: Save the shapefile

  • Click ‘OK’ to save the shapefile.
  • Turn off the layer created from importing the CSV file, leaving only the new shapefile layer visible.

Step 9: Verify the conversion

  • Check the location where the shapefile was saved to ensure it was created successfully.
  • Open the attribute table for the shapefile layer to confirm that the data from the CSV file has been transferred correctly.

How do I convert CSV files with non-spatial data (e.g., attributes) into shapefiles?

To convert a CSV file with non-spatial data (e.g., attributes) into a shapefile, you first need to have a spatial dataset (e.g., points, lines, or polygons) that you can link to the non-spatial data. You will then join the non-spatial CSV data to the spatial dataset based on a common attribute or unique identifier. Here’s a step-by-step guide using QGIS:

Prepare the CSV file:

  • Ensure the non-spatial CSV file has a column with a unique identifier that can be used to link it to the spatial dataset.

Prepare the spatial dataset (shapefile or other GIS format):

  • Make sure the spatial dataset also has a column with a unique identifier that matches the one in the non-spatial CSV file.

Import the spatial dataset and CSV file into QGIS:

  • Open QGIS and add the spatial dataset (shapefile) to the map canvas using the ‘Add Vector Layer’ button or by dragging and dropping it onto the map.
  • Go to the ‘Open Data Source Manager’ and select ‘Delimited Text’. Browse to the location of your CSV file and click ‘Open’. Make sure to specify the correct settings and click ‘Add’.

Join the non-spatial CSV data to the spatial dataset:

  • Right-click on the spatial dataset layer in the Layers panel and select ‘Properties’.
  • Go to the ‘Joins’ tab and click the ‘+’ button to add a new join.
  • In the ‘Add vector join’ dialog, select the non-spatial CSV layer from the ‘Join layer’ dropdown menu.
  • Choose the matching unique identifier columns in both the ‘Join field’ and ‘Target field’ dropdown menus.
  • Click ‘OK’ to create the join, and then close the layer properties window.

Export the joined data as a new shapefile:

  • Right-click on the spatial dataset layer in the Layers panel.
  • Choose ‘Export’ and then ‘Save Features As’.
  • In the ‘Format’ dropdown menu, select ‘ESRI Shapefile’.
  • Specify a file name and location for the new shapefile.
  • Click ‘OK’ to save the shapefile.

Now you have successfully converted a CSV file with non-spatial data into a shapefile by joining it to a spatial dataset.

How to convert CSV files with different coordinate systems into shapefiles with a common coordinate system

To do this, you’ll need to follow these steps using QGIS:

Import each CSV file into QGIS:

  • For each CSV file, import it into QGIS using the ‘Open Data Source Manager’ and follow the steps for importing a CSV file with spatial data (as described in the previous answers).

Set the appropriate Coordinate Reference System (CRS) for each layer:

  • For each imported CSV layer, ensure that the correct CRS is set. To do this, right-click on the layer in the Layers panel, select ‘Properties’, go to the ‘Source’ tab, and click the ‘Set CRS’ button to choose the appropriate CRS. Click ‘OK’ to apply the changes.

Reproject each layer to the desired common CRS:

  • Right-click on each layer in the Layers panel, choose ‘Export’, and then ‘Save Features As’.
  • In the ‘Format’ dropdown menu, select ‘ESRI Shapefile’.
  • Specify a file name and location for each new shapefile.
  • In the ‘CRS’ section, click ‘Select CRS’ and choose the desired common CRS for all the shapefiles.
  • Click ‘OK’ to save each reprojected shapefile.

Merge the reprojected shapefiles (optional):

  • If you want to combine the reprojected shapefiles into a single shapefile, use the ‘Merge Vector Layers’ tool. Go to the ‘Processing’ menu, then ‘Toolbox’, and search for ‘Merge Vector Layers’.
  • In the ‘Merge Vector Layers’ dialog, click the ‘…’ button next to ‘Input layers’ and select all the reprojected shapefiles.
  • Specify the file name and location for the merged output shapefile.
  • Click ‘Run’ to create the merged shapefile.

Now you have successfully converted CSV files with different coordinate systems into shapefiles with a common coordinate system.

Common errors or issues when converting CSV data to shapefiles

Incorrect or missing delimiter:

  • Ensure that the delimiter (e.g., comma, tab) used in the CSV file is correctly specified when importing the file into QGIS.

Incorrect formatting of coordinates:

  • Check that the coordinates in the CSV file are formatted correctly (e.g., decimals, not degrees-minutes-seconds) and that the correct columns are specified for X and Y coordinates when importing.

Incorrect or missing Coordinate Reference System (CRS):

  • Make sure to specify the correct CRS for the data when importing the CSV file and when exporting the shapefile. If the CRS is unknown, you may need to consult the data source or metadata for the correct CRS.

Missing or corrupt attribute data:

  • Inspect the CSV file to ensure that the attribute data is properly formatted and that there are no missing or corrupt values. Remove or fix any incorrect data before importing.

Encoding issues with special characters or non-English text:

  • When importing the CSV file, specify the correct character encoding (e.g., UTF-8) to avoid issues with special characters or non-English text.

Large file size or performance issues:

  • If the CSV file is very large, the conversion process may be slow or cause performance issues in QGIS. In such cases, consider splitting the CSV file into smaller parts, converting them separately, and then merging the resulting shapefiles.

Invalid geometry:

  • If the CSV file contains invalid geometry (e.g., duplicate points, self-intersecting lines, or polygons), this may cause issues during conversion. You may need to clean or correct the geometry using QGIS’s geometry validation tools.

To resolve these issues, carefully review the data in the CSV file, ensure that the correct settings are specified when importing the file into QGIS, and use QGIS’s built-in tools to clean or correct any issues with the data or geometry.

How To convert CSV files containing lines or polygons into shapefiles, not just points!

To convert a CSV file with lines or polygons into a shapefile, you can use a combination of QGIS and a scripting language such as Python with the help of libraries like geopandas and shapely.

Here’s an example of how to convert a CSV file containing polyline data into a shapefile using Python:

Prepare the CSV file:

  • Ensure the CSV file contains the necessary information to create lines or polygons, such as a unique identifier for each feature and coordinate pairs in separate columns (e.g., X1, Y1, X2, Y2).

Install the required Python libraries:

  • Install the geopandas, pandas, and shapely libraries, if not already installed, using pip:
pip install geopandas pandas shapely

Create a Python script to convert the CSV data to a shapefile:

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString

# Read the CSV file
csv_file = 'path/to/your/csv_file.csv'
data = pd.read_csv(csv_file)

# Define a function to create LineString objects from coordinate pairs
def create_line(row):
    coords = [(row['X1'], row['Y1']), (row['X2'], row['Y2'])]
    return LineString(coords)

# Apply the function to create a new 'geometry' column containing LineString objects
data['geometry'] = data.apply(create_line, axis=1)

# Convert the DataFrame to a GeoDataFrame and set the CRS
gdf = gpd.GeoDataFrame(data, crs="EPSG:4326")

# Save the GeoDataFrame as a shapefile
output_shapefile = 'path/to/output/shapefile.shp'
gdf.to_file(output_shapefile)

Replace 'path/to/your/csv_file.csv' and 'path/to/output/shapefile.shp' with the appropriate file paths for your input CSV and output shapefile, respectively. Also, adjust the EPSG code in the crs parameter to match your data’s coordinate system.

For polygons, you would need to modify the script to create Polygon objects instead of LineString objects using the shapely.geometry.Polygon class.

After running the Python script, you will have a shapefile containing the line or polygon features from the CSV file. You can then open this shapefile in QGIS or any other GIS software for further analysis or visualization.

How to automate the process of converting CSV data to shapefiles using Python

Yes, you can automate the process of converting CSV data to shapefiles using Python or other scripting languages. In this example, we’ll use Python with the libraries geopandas, pandas, and shapely to convert CSV data containing point coordinates into a shapefile.

Install the required Python libraries, if not already installed:

pip install geopandas pandas shapely

Create a Python script to convert the CSV data to a shapefile:

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

# Read the CSV file
csv_file = 'path/to/your/csv_file.csv'
data = pd.read_csv(csv_file)

# Create a function to create Point objects from coordinate pairs
def create_point(row):
    return Point(row['longitude'], row['latitude'])

# Apply the function to create a new 'geometry' column containing Point objects
data['geometry'] = data.apply(create_point, axis=1)

# Convert the DataFrame to a GeoDataFrame and set the CRS
gdf = gpd.GeoDataFrame(data, crs="EPSG:4326")

# Save the GeoDataFrame as a shapefile
output_shapefile = 'path/to/output/shapefile.shp'
gdf.to_file(output_shapefile)

Replace 'path/to/your/csv_file.csv' and 'path/to/output/shapefile.shp' with the appropriate file paths for your input CSV and output shapefile, respectively. Also, adjust the EPSG code in the crs parameter to match your data’s coordinate system.

You can further automate this script to process multiple CSV files by using loops and file manipulation functions in Python. This script can be run as a standalone script or integrated into a larger project for batch processing or other purposes.

For lines and polygons, you can follow a similar approach, as demonstrated in the previous answer, to create LineString or Polygon objects using the shapely library.

In conclusion

Converting CSV data to shapefiles is a crucial step in many geospatial workflows. By utilizing QGIS and Python, you can efficiently transform your data and tackle various scenarios, including point, line, and polygon geometries. Additionally, you can overcome common issues and automate the process to streamline your work. Happy mapping!

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.