1

I have the requirement to convert a list of ~100 KML files into shapefiles and later merge and upload the polygons as one layer into ArcGIS. I'm new to GIS and Python.

I've been able to convert multiple KML files in a directory into ESRI Shapefiles (.shp, .shx, .prj, .dbf) within the same directory by installing ogr2ogr in Conda and running this in Command Line:

<strike>for %f in (*.kml) do ogr2ogr -f "ESRI shapefile" "%f"</strike>

EDIT (correct code):

for %f in (*.kml) do ogr2ogr -f "ESRI Shapefile" "%~nf.shp" "%f"

enter image description here

This results in the following:

enter image description here

How can I add onto this to move the output files to a separate folder similar to this: C:\\Users\\Desktop\\DAT Files\\Redshift Data Testing\\Output as well as zip each group of .shp, .shx, .prj, .dbf by location (Cherry Hill, State College, etc.) similar to this: enter image description here

As a follow-up question, would it be possible to create a Python program to call and perform these actions?

I will table the requirements to merge these outputs and upload into ArcGIS for now as just getting the above down would be a HUGE win for me! (I also can perform that manually for now)

The post that I used for reference was: Using ogr2ogr to convert all shapefiles in directory?

3
  • for %f in (*.kml) do ogr2ogr -f "ESRI shapefile" "%f" this code does not give shapefiles.
    – neogeomat
    Commented Aug 5, 2019 at 18:26
  • for %f in (*.kml) do ogr2ogr -f "ESRI shapefile" "%f.shp" %f this worked
    – neogeomat
    Commented Aug 5, 2019 at 18:34
  • @neogeomat - I apologize, I uploaded the wrong code. Here is the one I actually used: for %f in (*.kml) do ogr2ogr -f "ESRI Shapefile" "%~nf.shp" "%f" Commented Aug 5, 2019 at 18:39

1 Answer 1

0
  1. to output to the output folder, add folder name like this for %f in (*.kml) do ogr2ogr -f "ESRI shapefile" "output\%f.shp" %f
  2. Shapefile is a multi-file format, and OGR updates the files in a non-sequential way. You have to zip it afterwards.
  3. Merging can also be done using -append,-update. Refer to this

Not the answer you're looking for? Browse other questions tagged or ask your own question.