<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Rubens on Medium]]></title>
        <description><![CDATA[Stories by Daniel Rubens on Medium]]></description>
        <link>https://medium.com/@danielrubens?source=rss-32d992cc0c2e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*nnYr4K8LIb3JEaSr2cfyAw.png</url>
            <title>Stories by Daniel Rubens on Medium</title>
            <link>https://medium.com/@danielrubens?source=rss-32d992cc0c2e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:03:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielrubens/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Python + QGIS: batch conversion of KML’s in shapefiles and merging into a single vector]]></title>
            <link>https://medium.com/@danielrubens/python-qgis-batch-conversion-of-kmls-in-shapefiles-and-merging-into-a-single-vector-f2d04be45e09?source=rss-32d992cc0c2e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2d04be45e09</guid>
            <category><![CDATA[merge]]></category>
            <category><![CDATA[convertion]]></category>
            <category><![CDATA[mlk]]></category>
            <category><![CDATA[pyqgis]]></category>
            <category><![CDATA[shapefile]]></category>
            <dc:creator><![CDATA[Daniel Rubens]]></dc:creator>
            <pubDate>Wed, 27 Nov 2019 00:15:38 GMT</pubDate>
            <atom:updated>2019-12-04T16:17:40.019Z</atom:updated>
            <content:encoded><![CDATA[<p>Using QGIS in the traditional way can be exausting when you need to do the same thasks several times. I remember the day when I spent almost one hour converting file by file from KML to shapefile, it was something like 200 KML’s that should be tourned into 200 shapefiles. After that day, I realized it was time to change the way I use QGIS. Using Python increased my produtivity and also decreased my stress and boriness at work. Acctually made me curious about all the oportunities and the projects I could create after that.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FLqNhCN0tJB9m8GE2pt%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fpyqgis-convertkmltoshapefile-mergeshapefile-LqNhCN0tJB9m8GE2pt&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FLqNhCN0tJB9m8GE2pt%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/6263be7950cdfe83bf56a1e80422bb4b/href">https://medium.com/media/6263be7950cdfe83bf56a1e80422bb4b/href</a></iframe><p>First the code will create the shapefiles, then they will be loaded at the vector layer.</p><p>You can download the KML’s with all the routes in Brazil by state clicking <a href="https://drive.google.com/open?id=1ig2PnlQ6PXxsqDe4PTwi5ensEnfm9trP"><strong>here</strong></a> or you can go the DNIT website(brazilian National Department of Transport Infrastructure) <a href="http://www.dnit.gov.br/mapas-multimodais/arquivos-kml"><strong>here</strong></a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a8c7263b6bb2281b86e2ea16fd1b519/href">https://medium.com/media/9a8c7263b6bb2281b86e2ea16fd1b519/href</a></iframe><p><strong>Line 4: </strong>depending on your Python version, you may use PyQt5.QtCore or PyQt4.QtCore. So, if you have a error message showing this line of code, just change the version of PyQt.</p><p>From <strong>line 8 </strong>to <strong>line 10</strong> I just set the project paremeters, always the same to any Qgis+Python project you may use.</p><p><strong>Line 12: </strong>the directory where I had my KML files to be converted to shapefile and the variable <strong>nome </strong>as a list.</p><p><strong>Line 13:</strong> reading file by file from the directory at line 12.</p><p><strong>Line 14: </strong>considering the variable <strong>kmls</strong> as any file that ends with <strong>.kml</strong>.</p><p>After these commands, every kml file will have it’s shapefile version.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c426db77fe68be56ee770345839ff9da/href">https://medium.com/media/c426db77fe68be56ee770345839ff9da/href</a></iframe><p>The main difference between KML and Shapefile in QGIS is that shapefile is editable and kml is not. So, the advantage of this conversion is the possibility of change, delete and agregate the information. The idea that I liked most was merging all the shapefiles into a single.</p><p>This part of code I got from <a href="https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html"><strong>here</strong></a>, where I found a world of possibilities to use Python+GDAL.</p><p><strong>outputMergefn</strong> is the variable related to the shapefile called <strong>rodovias.shp, </strong>and on this variable I stored the informations of all the others shapefiles.</p><p><strong>directory</strong> is the variable where I save the shapefile <strong>rodovias.shp</strong>. I already declared this same directory but with a different variable (in case someone using this could want save the files in another directory)</p><p><strong>fileStartsWith</strong> = ‘rod’ and <strong>fileEndsWith</strong> = ‘.shp’ are the patterns I need: every file fitting on this condition will be merged, but only be possible if every file has the same geometry (that’s why the variable <strong>geometryType</strong>=ogr.wkbLineString)</p><p><strong>Lines 38 to 40: </strong>setting the data source configuration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb48abe44db04ddbe05e34b4f247cf53/href">https://medium.com/media/bb48abe44db04ddbe05e34b4f247cf53/href</a></iframe><p>Here is the complete code. And now our final result.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Ffi_l6AZqIa4%3Ffeature%3Doembed&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dfi_l6AZqIa4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Ffi_l6AZqIa4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/25e4cd625f5a5b00845e380dbfeee122/href">https://medium.com/media/25e4cd625f5a5b00845e380dbfeee122/href</a></iframe><p>Your final screen must be like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LGNjXOc64km5YjI2Brd9lQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2d04be45e09" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python + QGIS: generating multiple shapefiles]]></title>
            <link>https://medium.com/@danielrubens/python-qgis-generating-multiple-shapefiles-1629bc609092?source=rss-32d992cc0c2e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1629bc609092</guid>
            <category><![CDATA[shapefile]]></category>
            <category><![CDATA[generatemultipleshapefile]]></category>
            <category><![CDATA[qgis]]></category>
            <category><![CDATA[pyqgis]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Rubens]]></dc:creator>
            <pubDate>Mon, 11 Nov 2019 23:57:08 GMT</pubDate>
            <atom:updated>2019-11-12T13:17:44.204Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p-KQOg6_YEwUnxkTZnuW2g.png" /></figure><p>The integration between QGIS and Python makes the use and processing of geospatial data amazing. Some activities that could be exausting (like clicking multiple times to do the same tasks over and over) can get optimized and be really fun. Never thought I would say the word “fun” referring to a programming language, but Python + QGIS made me do it.</p><p>You can download the shapefile on <a href="https://github.com/danielrubens/PyQGISmultipleshapefile/blob/master/BR.zip">my github profile</a>, or just click in <a href="https://github.com/danielrubens/PyQGISmultipleshapefile/raw/master/BR.zip">here.</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3549263b980ab46c76de425b89da11a5/href">https://medium.com/media/3549263b980ab46c76de425b89da11a5/href</a></iframe><p>As you can see, the code for this example is really simple.</p><p>At line 9, I defined the variable BRAZIL as being on the adress “C:\Users\daniel.rubens\Desktop\project\BR.shp”.</p><p>The BR.shp is part of the shapefile that contains information about all the states in Brazil and the regions they belong to. The shapefile comes with the following archives:</p><p>BR.cpg, BR.dbf, BR.prj, BR.shp, BR.shx<br>All of them must be on the same folder.</p><p>At line 10, by the command<strong> iface.addVectorLayer</strong> I called the variable BRAZIL, then put the same name to be showed up as my layer, and <strong>“ogr”</strong> is the Esri format contained at the shapefile.</p><p>At line 13 I created a shapefile called<strong> GEO_SIRGAS</strong> that will be our CRS (coordinate referenced system) and at line 14 I defined that the reference will be take out of the shapefile BRAZIL and the EPSG will be the 4674.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9w4nHUj1-gtEEbgCFCfGsQ.png" /></figure><p>Openning the attribute table on QGIS, you will see NM_ESTADO (with every state in Brazil) and also NM_REGIAO (with the regions). Well, I choosed the NM_REGIAO, but the principle is the same.</p><p>Based on the attributes on NM_REGIAO, the line 17 has the variable with this names:</p><p>regioes =[“NORTE”, “NORDESTE”, “SUL”, “SUDESTE”, “CENTRO-OESTE”]</p><p>After I defined the names in the variable “regioes”, I used the command “<strong>extractbyattribute</strong>” to generate new shapefiles based on the attributes inside the variable “regioes”.</p><p>Here’s our final result.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTaqSt6wMypc%3Ffeature%3Doembed&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTaqSt6wMypc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTaqSt6wMypc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7d811620b9b27c45da687969bb3f0bd/href">https://medium.com/media/b7d811620b9b27c45da687969bb3f0bd/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1629bc609092" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>