Converting The Layers of a DWG File in AutoCAD to an ESRI Shapefile using GeoPackages in QGIS 3.x

Victor E. Irekponor
7 min readMay 26, 2020

I have noticed that there is not much easy to follow documentation for open source Geospatial analytic operations, the documentations that one may likely find are either outdated, do not work for one’s use-case, or too technical to follow along.

As a result of that, I have taken it upon myself to put out as many tutorials as possible for any GIS inclined project I work on and of course, it would also be beneficial to me in the long run as I can always come back here to make references in future peradventure I forget any of these concepts, so this is going to be a series. I would also try to adopt a very intuitive and simplistic approach in this tutorial and of course I am always available on my social media handles Twitter, LinkedIn, Facebook, and Instagram so feel free to connect and shoot your questions.

TL;DR

Say you have an AutoCAD drawing (of .dwg file extension), this could be a layout plan or a just a simple drawing that has a number of layers that you want to export to a GIS application and convert it to an ESRI shapefile for more advanced processing. How would you go about that? Well, keep reading!

Want to read this story later? Save it in Journal.

For this tutorial, we would be using the QGIS software, simply because it is opensource, has a relatively gentle learning curve, and also very easy to install on either Windows, Ubuntu, or Mac operating systems (from experience).

I would be assuming you already have a version of AutoCAD installed on your PC, because how would you access a *.dwg file in the first place without the CAD software. For non-computer/tech-savvy readers, the asterisk means the filename — it could be any file name.

But if you do not have QGIS already set up, this tutorial here pretty much does a very good job of helping you set that up quickly. Let us take a short break while you go and do that.

Now that you are back and should already have QGIS installed on your windows, Mac or Linux powered machine. Get set to run through the following steps.

First, open up the *.dwg file in AutoCAD and convert it into a *.dxf file. A DXF is a format created by Autodesk to facilitate the translation of CAD data from other programs to AutoCAD or vice versa, this matches our use case since we are trying to transfer data from AutoCAD to QGIS software. To do this simple conversion, we navigate to file>>>Save As, then type in the file-name and select the right extension you want from the drop-down menu which is .dxf as shown in the image below.

The image above shows the viewport of AutoCAD version 2017 and a dialogue box to save the drawing file to a new file having the dxf file extension.

Now as that is done and out of the way. Open up the QGIS software, and as soon as it is loaded, you should see an empty canvas, similar to the image below.

What an empty QGIS canvas looks like as soon as it is launched. Until you create a new project

Then you navigate to Project>>>Import/Export>>>Import Layers from DWG/DXF as shown in the image screenshot below.

The Image above shows graphically how to navigate to import layers from the AutoCAD file to GIS from the project menu.

As soon as you click on that, a dialogue box should appear similar to what you see below for importing the drawing into an OGC GeoPackage.

The image above shows a QGIS dialogue box where you have to initialize an empty gpkg file that would contain the layers from the AutoCAD DXF file amongst others.

If you do not already know what a GeoPackage is, here are good resources explaining what they are. [1], [2],[ 3].

The GeoPackage is the GIS file format of the future, and the future is now. As you may have noticed QGIS 3.x switched from shapefiles to GeoPackage as default in most cases. Thus, rendering gets really fast. Loading, panning, and zooming feel way quicker with GeoPackage than with Shapefiles.

Per the most recent image above, in the Target Package box, you are to initialize an empty GeoPackage (.gpkg) layer where you would store all the separate layers from your DXF file, so left-click on the small box icon to the right of Target Package, just before Load Layers, and navigate to the specific directory where you want your .gpkg file to be saved and add a file name to it.

Next, you need to make sure the coordinate reference system is correct, but if it isn’t then it is your prerogative to do the needful.

As soon as that is done and out of the way, you go ahead to import the source drawing whose layers you want to store in a geoPackage, If you have been following, you would note that this drawing should be of type DXF file format. So just click on Import, grab the initial *.dwg file you converted to *.dxf in the first step and click on OK to start importing your layers. It should take a while depending on the file size and your compute power.

As soon as that is done importing, you should have something similar to the image below.

Image showing what the dialogue box looks like after the layers have been imported from the *dxf file and now stored in a gpkg file format.

You can now decide to tick or untick all the layers you want to keep or discard, give them a group name, and then click OK to signify that you are done.

Wooohooo!!!

You just successfully imported your DWG file to QGIS as a geoPackage layer which can now be viewable in the canvas. See the image below.

The image above shows a QGIS canvas with the newly imported vector file from AutoCAD as well as its individual layers, this drawing can now be subjected to further analysis.

Now that we have the AutoCAD drawing in QGIS as individual geoPackage layers it is worthy to note that they can still be further converted to ESRI shapefiles if you want by simply right-clicking on the layer>>>Export>>>Save Feature As as shown in the image below.

Navigation steps to export the gpkg layers to ESRI shapefile formats.

Left-click on Save Feature As and another dialogue box should pop-up where you can select the ESRI shapefile format from the list of file formats, specify a file name, and click on OK. See the image below for more reference to this step.

CAVEAT! The file name should be an explicit path to the file, in order to do that, left-click on the small box icon to the right of the file name text-box and navigate to your working directory where you can then specify the shapefile name.

So that, my friend, is how you convert an AutoCAD drawing to an editable ESRI shapefile vector for further analysis in QGIS.

Mind you there are other ways this could be done on ArcGIS using ArcPy, and with GDAL’s ogr2ogr. But I found this method to be more straightforward. Plus it worked very well for me.

In subsequent editions of this series, I would actually be exploring other options for converting layers from a DWG file to an editable ESRI shapefile vector format. More likely with an automating tool like Python because what if I had a 1000 or even a 100 layers to export to an ESRI shapefile format. A batch-wise operation would make life easier in such cases.

However, One other thing to note is that though the layers have been converted to a shapefile, they still need to be georeferenced. With Raster data georeferencing is quite straightforward. But with Vector data like shapefiles, there are a number of ways one might go around that including rasterizing the vector, or doing Affine transformations with known Ground Control Points (GCPs) using GDAL’s ogr2ogr, or pyQGIS, ArcPy, Fiona, basically any of the open-source frameworks suited for this purpose.

The next post would primarily be on GeoReferencing a vector GIS shapefile using Ground Control Points (GCPs) and Affine Transformations.

Let me know if you have any questions in the comment section, or a more effective way to go about this task. I would be happy to take your questions and also learn.

Also, all the DWG, DXF, SHP, and GPKG files and images used in the post have been uploaded to my GitHub repository here, It is open-source now. So feel free to access it and play with it!!

📝 Save this story in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Victor E. Irekponor

Ph.D student @ Center for Geospatial Information Science (CGIS), University of Maryland, College Park | Spatial Data Scientist | GeoAI | Autodidact