6

Convert KML/KMZ to CSV or KML/KMZ to shapefile or KML/KMZ to Dataframe or KML/KM...

 1 year ago
source link: https://gist.github.com/linwoodc3/0306734dfe17076dfd34e09660c198c0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Okay @lkwalke4, I'll take a look.

Setting the Stage and Expectations

Upfront, I apologize for the lengthy explanations. This response provides instruction in detail because I want to make sure you have everything you need, but I also want to have this instruction for anyone that would come after you. If the help seems overly simple or extensive, keep in mind the purpose. I'm want to write this so any level of Python user can understand.

Also, understand it will be difficult for me to debug your exact issue since I don't have the file; it's like I'm trying to reach a door on the other side of a dark room that I vaguely remember. Expect some stumbles along the way :-) . Some details below may not apply to you, but I'll give it my best go. Let's get to your solution!

The First Solution: Adding a New Line to Skip the Error

Based on your error, there's one bad key. You can skip it to see if you're getting the result you need. You'll make an edit at one specitic line; line 103-105. Just add this to skip:

elif self.inPlacemark:
    if self.name_tag != "" and name in self.mapping[self.name_tag]:
        self.mapping[self.name_tag][name] += self.buffer
    elif self.name_tag != ""
        self.mapping[self.name_tag][name] = self.buffer
self.buffer = ""

Try that, and if it works, awesome! If not, paste the stack trace.

Another Solution: Use geopandas

There's another option too! You could just use geopandas. The instructions on how to read a KML/KMZ in geopandas were inspired from this post.

First you need to install geopandas and make sure it's installed in the Python interpreter that you use for ArcGIS. Based on your stack trace, you're on a Windows machine to installs can be tricky. Use wheels to make it simpler. geopandas requires fiona,pyproj and shapely, and those libraries require GDAL and GEOS (You may know about these libraries as they are core libraries in GIS. You also may need to install from here if below doesn't work). l Visit this page which has Python wheels for windows machines, and install wheels for (links below are clickable):

Make sure you download the correct wheel for your version of Python and architecture of Windows.

Installing the Wheels after downloading

Installing those wheels is easy.

  1. Download the wheels and remember the folder where you saved them.
  2. Open up a command prompt on your Windows machine. T
  3. Navigate, in your command prompt, to the folder/directory where you downloaded the wheels. You can use this easy shortcut to open the prompt quickly without using the command line if you're unfamiliar with command line syntax.
  4. Then just pip install that wheel file like so: pip install Fiona‑1.7.12‑cp34‑cp34m‑win_amd64.whl
  5. Do that for each wheel
  6. Then try to import geopandas; if it works without error, you're good!! import geopandas as gpd

The next step is enabling KML reading in fiona (fiona is the Python library to read and write geospatial files).

fiona.drvsupport.supported_drivers['kml'] = 'rw' # enable KML support which is disabled by default
fiona.drvsupport.supported_drivers['KML'] = 'rw' # enable KML support which is disabled by default

And now you just read in your KML (if you had a KMZ, extract it first to get your KML file, which is usually named doc.kml):

import geopandas as gpd
a = gpd.read_file("/path/to/your/doc.kml")

As a warning, this method is easy but sometimes you lose data (not all lines read in). Because it's so easy, I think it's at least worth a shot.

That code will load up your KML as a geopandas.GeoDataFrame; geopandas is a POWERFUL geospatial analysis library in Python. It eliminates your need for PostGIS; with a stack of geopandas, shapely (for geosptial ops), pysal (for advanced geospatial analytics), rasterio (raster analysis), datashader (visualize large datasets in milliseconds such as lidar) and fiona, you eliminate the need for ArcGIS or any other GIS tool.

Moving on, the last step is writing your data to a shapefile. geopandas can write to any file that fiona writes to and that list is extensive. This line of code exports the GeoDataFrame as an ESRI Shapefile named test.shp.

a.to_file('test.shp',driver='ESRI Shapefile')

That line also creates the dbf, prj,cpg,shx and gif files.

Final Suggestion if All Else Fails: use QGIS

If you're having trouble reading KMZ/KML in ArcGIS, I honestly suggest using QGIS. QGIS is, in my humble opinion, better than ArcGIS at dealing with different types of data formats and it is better at handling large data files (ArcGIS will choke on data sets that QGIS handles easily). And, best of all, QGIS is free.

Just follow download/install instructions here: https://qgis.org/en/site/forusers/download.html#

A zoologist in South Africa used this same code I'm helping you with. We got his errors fixed so I'm confident we can do the same for your KML. If not, there's QGIS. QGIS would require manual loading and saving, but his complex tracking data loaded in QGIS with zero issues. You'd just load, right click and download as a shapefile:

kmzcomplex

Get back to me when you can. Sorry it took a few days to respond but hopefully you can try some of the things above and get back to me in the next week or so.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK