How to Convert GIS Data with Python: KML To Layer

المشرف العام

Administrator
طاقم الإدارة

0000

Within our GIS works we often find the need to transform the information from the start or delivery to the required work format: from CAD to Shapefile, from Shapefile to Kml, from Geodatabase to Shapefile, GPX to Feature, etc.
In ArcGIS, all options GIS information processing are contained in the Tools called Conversion Tools .
When the volume of the information to be handled is important it is advisable to program the task so that all the information of the indicated type that is located in a specific directory is transformed into the required format.
With ArcPy we can use any of the tools we see in ToolBoxes in Python code mode, which we can also enrich with loops, conditions, parameters etc.
In this case study we will make a script for converting GIS data with Python and ArcPy , to create a Geodatabase and layer file (.lyr) for each type kml files to be located in a specific directory.
To carry it we just need to know:

  • The syntax of the tool kml to layer : ArcPy
  • Create a list of all kml files in a directory: ArcPy or Python
  • Make a FOR-type loop that traverses that list and executes the tool: Python
A few notes about ArcPy
In our article What it is ArcPy we explain this library. By way of summary you should know that ArcPy is a Python library created specifically by ESRI to program and automate GIS processes. Contains Classes, Functions, etc. Specific to ESRI, which can be combined with Python's own functionalities as well as with other libraries in this language.

Index [ close ]​

  • How to get the code
    • 1 The syntax of the kml to layer tool
    • 2.- Create a list of all files type kml of a directory.
    • 3.- Make a FOR-type loop that traverses that listing and runs the tool
  • Transcribing and ordering the code: We created the script
  • Hands on: Run the script
  • Possible improvements and code reuse

[h=1]How to get the code[/h]We have several options to get the code we need. To begin with , all the tools of ArcGIS Toolboxes are accompanied by corresponding Help , which you can access either through the right button on the tool, or in Help button in the dialog box that tool.

We also have the option via the Web, both in specific forums, for example [url]https://geonet.esri.com/community/developers/gis-developers/python[/URL] , as in the search for ArcGIS Desktop: http: // desktop .arcgis.com / en /
Luckily the documentation provided by ESRI is very simple, clear and structured. In addition it is always accompanied by a practical example that we can reuse and adapt to our needs, although obviously a greater degree of knowledge and skill with the programming will allow us to take more advantage of the ArcPy.
[h=2]1 The syntax of the kml to layer tool[/h]We look for the tool and access your help as we see in the previous image. It will open a help page with different sections: Sumary, Usage, Syntax, Code Sample ... For us these last two are the most important, with them we can see how the tool translates to Python code and an applied example.

As we see in the aid, the sentence we need is:

The statements for running GIS tools always maintain the same structure:
arcpy + point + specific module (if available ) + name + tool _apellido tool (if any) + (parameters in brackets separated by commas, {optional parameters})
In our case, we will dispense with the optional parameters, so our sentence will be:


1

[COLOR=#002D7A !important]arcpy
[COLOR=#333333 !important].
[COLOR=#004ED0 !important]KMLToLayer_conversion[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#333333 !important]([/COLOR][COLOR=#002D7A !important]in_kml_file[/COLOR][COLOR=#333333 !important],[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]output_folder[/COLOR][COLOR=#333333 !important])[/COLOR]
[/COLOR]

[/COLOR]

The parameter value in_kml_file would assign through the list of files that generate kml.
The value of output_folder we can assign as a variable at the beginning of the script.
[h=2]2.- Create a list of all files type kml of a directory.[/h]For this point we will use ESRI web browser.
In the following link: [url]https://pro.arcgis.com/es/pro-app/arcpy/get-started/listing-data.htm[/URL] we see the different possibilities of listings of data that can be generated through ArcPy. Since a kml file type is not a specific type of ESRI, within the possible tools it seems that the best will be to ListFiles , using the filter file extension. (* .kmz for our example)
We access the specific help of this tool to see its syntax and reuse its code:

The syntax we need is simple:


1

[COLOR=#002D7A !important]arcpy
[COLOR=#333333 !important].
[COLOR=#004ED0 !important]ListFiles[/COLOR][COLOR=#333333 !important]([/COLOR][COLOR=#008000 !important]'*.kmz'[/COLOR][COLOR=#333333 !important])[/COLOR]
[/COLOR]

[/COLOR]

As explained in its help is an essential requirement that prior to its execution has been established as workspace the directory from which the listing is wanted. As we see in the example this is done with the following statement:


1

[COLOR=#002D7A !important]arcpy
[COLOR=#333333 !important].
[COLOR=#002D7A !important]env[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#002D7A !important]workspace[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#006FE0 !important]=[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#008000 !important]'D:/temp/archivos_kml'[/COLOR]
[/COLOR]

[/COLOR]
This statement could have been done through the module you Python without using ArcPy.
With the code we see in the example of the tool we can address our third objective.
[h=2]3.- Make a FOR-type loop that traverses that listing and runs the tool[/h]The creations of listings are usually accompanied by structures of type FOR, which cross the list in order to operate with its elements.
In the example we see in the tool support ListFiles the FOR loop is applied to create a dbf file type from each of the types listed csv files.
Will copy this example and replace these steps for our tool conversion. The syntax of the loop would be:


1

[COLOR=#800080 !important]for
[COLOR=#006FE0 !important]
[COLOR=#004ED0 !important]in_kml_file [/COLOR][COLOR=#800080 !important]in[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]arcpy[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#004ED0 !important]ListFiles[/COLOR][COLOR=#333333 !important]([/COLOR][COLOR=#008000 !important]"*.kmz"[/COLOR][COLOR=#333333 !important])[/COLOR][COLOR=#006FE0 !important]:[/COLOR]
[/COLOR]

[/COLOR]

[h=1]Transcribing and ordering the code: We created the script[/h]OKAY! We have the code, now we just have to sort it

You can write your code in an IDE ( Integrated Development Environment ), which facilitates the transcription, debugging and execution. Our recommendation is to use PyScriter .
Now, let's go in order, and accompany each step of a comment (in Python comments are preceded by a # symbol), so that the intent of each statement is clear:


1
[COLOR=#AFAFAF !important]2

3
[COLOR=#AFAFAF !important]4[/COLOR]
5
[COLOR=#AFAFAF !important]6[/COLOR]
7
[COLOR=#AFAFAF !important]8[/COLOR]
9
[COLOR=#AFAFAF !important]10[/COLOR]
11
[COLOR=#AFAFAF !important]12[/COLOR]
13
[COLOR=#AFAFAF !important]14[/COLOR]
15
[COLOR=#AFAFAF !important]16[/COLOR]
17
[COLOR=#AFAFAF !important]18[/COLOR]
19
[COLOR=#AFAFAF !important]20[/COLOR]
21
[COLOR=#AFAFAF !important]22[/COLOR]
23
[COLOR=#AFAFAF !important]24[/COLOR]
25

[COLOR=#FF8000 !important]#importamos arcpy para poder trabajar con esta biblioteca


[COLOR=#800080 !important]import[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]arcpy[/COLOR]

[COLOR=#FF8000 !important]#variables de trabajo: directorio de entrada y de salida[/COLOR]

[COLOR=#002D7A !important]in_folder[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#006FE0 !important]=[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#008000 !important]'D:/temp/archivos_kml'[/COLOR]

[COLOR=#002D7A !important]output_folder[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#006FE0 !important]=[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#008000 !important]'D:/temp/archivos_gdb'[/COLOR]

[COLOR=#FF8000 !important]#establecemos el workspace, que es el mismo que el directorio de entrada[/COLOR]

[COLOR=#002D7A !important]arcpy[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#002D7A !important]env[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#002D7A !important]workspace[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#006FE0 !important]=[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]in_folder[/COLOR]

[COLOR=#FF8000 !important]#bucle FOR y listado de archivos tipo kmz[/COLOR]

[COLOR=#800080 !important]for[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#004ED0 !important]in_kml_file [/COLOR][COLOR=#800080 !important]in[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]arcpy[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#004ED0 !important]ListFiles[/COLOR][COLOR=#333333 !important]([/COLOR][COLOR=#008000 !important]"*.kmz"[/COLOR][COLOR=#333333 !important])[/COLOR][COLOR=#006FE0 !important]:[/COLOR]

[COLOR=#FF8000 !important]#herramienta de conversion de kml a capa[/COLOR]

[COLOR=#002D7A !important]arcpy[/COLOR][COLOR=#333333 !important].[/COLOR][COLOR=#004ED0 !important]KMLToLayer_conversion[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#333333 !important]([/COLOR][COLOR=#002D7A !important]in_kml_file[/COLOR][COLOR=#333333 !important],[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]output_folder[/COLOR][COLOR=#333333 !important])[/COLOR]

[COLOR=#800080 !important]print[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#002D7A !important]in_kml_file[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#006FE0 !important]+[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#008000 !important]" a capa...."[/COLOR]

[COLOR=#800080 !important]print[/COLOR][COLOR=#006FE0 !important] [/COLOR][COLOR=#008000 !important]"SCRIPT EJECUTADO CORRECTAMENTE"[/COLOR]
[/COLOR]

[/COLOR]

[h=1]Hands on: Run the script[/h]Although we have several options to run our code , it is recommended in a script like this, composed of several lines of code, it is to use your own editor: PyScripter.

In the image we can see the executed script and the messages returned in the Interpreter window of the program. From what we see it has been executed correctly, and so we can check it in the contents of the folder.

[h=1]Possible improvements and code reuse[/h]This implementation has been very useful. Without needing to open ArcGIS we have transformed layer massively type files kmz. In the same way, by changing the tool, we can do other types of data conversions:

  • FeatureClassToShapefile_conversion
  • FeaturesToJSON_conversion
  • GPXToFeatures_conversion,
  • etc.
The possibilities are as many as the options offered by ArcGIS, with the advantage of being able to run it for a large number of files and outside the ESRI environment. You only have to consider two things:

  • The type of tool to perform the listing will depend on whether the files are ESRI layers or other formats.
  • The parameters that each of the conversion tools considers mandatory.
In addition , the program can be improved by combining this code with other actions such as extracting statistics, make a series of maps with the imported information, generate a report, schedule the task, etc ... the possibilities are limitless.
Learn ArcPy in our online course Python for ArcGIS .


-- ADD YOUR CONTENT HERE --


أكثر...
 
أعلى