Converting the layers of a dwg file in autocad to an esri shapefile using geopackages in qgis 3. X

Converting the layers of a dwg file in autocad to an esri shapefile using geopackages in qgis 3. X


Play all audios:


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. 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. Then you navigate to PROJECT>>>IMPORT/EXPORT>>>IMPORT LAYERS FROM DWG/DXF as shown in the image screenshot below. 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. 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. 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. 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. 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. 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.