Code Monkey home page Code Monkey logo

kml.jl's Introduction

KML.jl

Working with Google Earth's KML format in Julia.

This package takes inspiration from Python's simplekml package.



Quickstart

Writing

file = KMLFile(
    Document(
        Features = [
            Placemark(
                Geometry = Point(coordinates=(77.0369, 38.9072)),
                name = "Washington, D.C."
            )
        ]
    )
)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Placemark>
      <name>Washington, D.C.</name>
      <Point>
        <coordinates>77.0369,38.9072</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

Reading

path = download("https://developers.google.com/kml/documentation/KML_Samples.kml")

file = read(path, KMLFile)

Writing

KML.write(filename::String, kml_file)  # Write to file

KML.write(io::IO, kml_file)  # Write to IO stream

KML.write(kml_file)  # String


KML Objects ←→ Julia structs

This package is designed to be used intuitively alongside Google's KML Reference Page. Thus, there are rules that guide the mapping between KML (XML) Objects and Julia structs.

  1. In Julia, each Object is constructed with keyword arguments only.
  2. Keywords are the associated attributes as well as child elements of the Object
    • E.g. pt = Point(id="mypoint", coordinates=(0,1)) sets the id attribute and coordinates child element.
  3. Every keyword has a default value (most often nothing). They can be set after construction.
    • E.g. pt.coordinates = (2, 3)
  4. If a child element is itself an Object, the keyword matches the type name.
    • E.g. pl = Placemark(); pl.Geometry = Point(). Here, a Placemark can hold any Geometry, which is an abstract type. A Point is a subtype of Geometry.
  5. Some Objects can hold several children of the same type. Fields with plural names expect a Vector.
    • E.g. mg = MultiGeometry(); mg.Geometries = [Point(), Polygon()]
  6. Enum types are in the KML.Enums module. However, you shouldn't need to create them directly as conversion is handled for you/helpful error messages are provided.
julia> pt.altitudeMode = "clamptoground"
ERROR: altitudeMode  clampToGround, relativeToGround, absolute
  1. Google extensions (things with gx: in the name) replace : with _.
    • E.g. gx:altitudeModegx_altitudeMode




For a concrete example, examine the fields of a KML.Document:

Fields
≡≡≡≡≡≡≡≡

id                 :: Union{Nothing, String}
targetId           :: Union{Nothing, String}
name               :: Union{Nothing, String}
visibility         :: Union{Nothing, Bool}
open               :: Union{Nothing, Bool}
atom_author        :: Union{Nothing, String}
atom_link          :: Union{Nothing, String}
address            :: Union{Nothing, String}
xal_AddressDetails :: Union{Nothing, String}
phoneNumber        :: Union{Nothing, String}
Snippet            :: Union{Nothing, KML.Snippet}
description        :: Union{Nothing, String}
AbstractView       :: Union{Nothing, KML.AbstractView}    # Camera or LookAt
TimePrimitive      :: Union{Nothing, KML.TimePrimitive}   # TimeSpan or TimeMap
styleURL           :: Union{Nothing, String}
StyleSelector      :: Union{Nothing, KML.StyleSelector}   # Style or StyleMap
region             :: Union{Nothing, KML.Region}
ExtendedData       :: Union{Nothing, KML.ExtendedData}
Schemas            :: Union{Nothing, Vector{KML.Schema}}  # Multiple Schemas allowed
Features           :: Union{Nothing, Vector{KML.Feature}} # Multiple Features (abstract type) allowed


kml.jl's People

Contributors

joshday avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kml.jl's Issues

Polygons from Google Earth not readable

Hello, I am trying to read files output from Google Earth Pro. I have sample output, a stack trace, and a possible solution

Sample input

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns:kml="http://www.opengis.net/kml/2.2">
<Document>
	<name>tmp.kml</name>
	<Placemark>
		<name>tmp</name>
		<styleUrl>#m_ylw-pushpin</styleUrl>
		<Polygon>
			<tessellate>1</tessellate>
			<outerBoundaryIs>
				<LinearRing>
					<coordinates>
						-50.80770360421555,27.50794679203254,0 -46.63827492868646,32.0230059721323,0 -54.85996847570588,35.7028706316008,0 -50.80770360421555,27.50794679203254,0 
					</coordinates>
				</LinearRing>
			</outerBoundaryIs>
		</Polygon>
	</Placemark>
</Document>
</kml>

Stack trace

julia> scenario = read("tmp.kml", KML.KMLFile)
ERROR: ArgumentError: cannot parse "0 -46.63827492868646" as Float64
Stacktrace:
  [1] _parse_failure(T::Type, s::SubString{String}, startpos::Int64, endpos::Int64)
    @ Base ./parse.jl:373
  [2] _parse_failure(T::Type, s::SubString{String})
    @ Base ./parse.jl:373
  [3] #tryparse_internal#509
    @ ./parse.jl:369 [inlined]
  [4] tryparse_internal
    @ ./parse.jl:366 [inlined]
  [5] #parse#510
    @ ./parse.jl:379 [inlined]
  [6] parse
    @ ./parse.jl:379 [inlined]
  [7] _broadcast_getindex_evalf
    @ ./broadcast.jl:683 [inlined]
  [8] _broadcast_getindex
    @ ./broadcast.jl:666 [inlined]
  [9] getindex
    @ ./broadcast.jl:610 [inlined]
 [10] macro expansion
    @ ./broadcast.jl:974 [inlined]
 [11] macro expansion
    @ ./simdloop.jl:77 [inlined]
 [12] copyto!
    @ ./broadcast.jl:973 [inlined]
 [13] copyto!
    @ ./broadcast.jl:926 [inlined]
 [14] copy
    @ ./broadcast.jl:898 [inlined]
 [15] materialize
    @ ./broadcast.jl:873 [inlined]
 [16] autosetfield!(o::KML.LinearRing, sym::Symbol, x::String)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:108
 [17] add_element!(o::KML.LinearRing, child::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:69
 [18] object(node::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:46
 [19] add_element!(o::KML.Polygon, child::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:65
 [20] object(node::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:46
 [21] add_element!(o::KML.Placemark, child::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:55
 [22] object(node::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:46
 [23] add_element!(o::KML.Document, child::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:55
 [24] object(node::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/parsing.jl:46
 [25] iterate
    @ ./generator.jl:47 [inlined]
 [26] _collect(c::Vector{XML.Node}, itr::Base.Generator{Vector{XML.Node}, typeof(KML.object)}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:802
 [27] collect_similar
    @ ./array.jl:711 [inlined]
 [28] map
    @ ./abstractarray.jl:3261 [inlined]
 [29] KML.KMLFile(doc::XML.Node)
    @ KML ~/.julia/packages/KML/UtUHK/src/KML.jl:157
 [30] read(filename::String, #unused#::Type{KML.KMLFile})
    @ KML ~/.julia/packages/KML/UtUHK/src/KML.jl:153
 [31] top-level scope
    @ REPL[5]:1

The autosetfield! function in parsing.jl appears to assume newlines between coordinate tuples which is not the case here. The following change works for me and appears to still work with Points. It includes spaces in the split and has a check for a single coordinate case. Original commented out.

    if sym == :coordinates
        val = [Tuple(parse.(Float64, split(v, ','))) for v in split(x)]
        if length(val) == 1
            val = val[1]
        end
        # val = occursin('\n', x) ?
        #     [Tuple(parse.(Float64, split(s, ','))) for s in split(x, '\n')] :
        #     [Tuple(parse.(Float64, split(x, ',')))]
        return setfield!(o, sym, val)
    end

Have a look and let me know if this solution works out. Thank you

KML.jl generates invalid XML

When writing a KML Document to a file, the XML header is generated in a wrong order leading to invalid XML files. The header is generated as <?xml encoding="UTF-8" version="1.0"?>, however the specification says, that the order has to be <?xml version="1.0" encoding="UTF-8"?>.
I assume this comes from the "attributes" being defined in a Dict, which is unorderes as far as I understood.

Node(XML.Declaration, nothing, Dict("version" => "1.0", "encoding" => "UTF-8")),

Error when encountering tags outside of the KML spec

I'm having an issue with the toolbox. I'm on Julia 1.9, with the current KML.jl and I have created a Placemark (in annex) and when I try to open with the toolbox I have the following error:

ArgumentError: invalid index: nothing of type Nothing
Stacktrace:
[1] to_index(i::Nothing)
@ Base .\indices.jl:300
[2] to_index(A::Vector{UInt8}, i::Nothing)
@ Base .\indices.jl:277
[3] _to_indices1(A::Vector{UInt8}, inds::Tuple{Base.OneTo{Int64}}, I1::Nothing)
@ Base .\indices.jl:359
[4] to_indices
@ .\indices.jl:354 [inlined]
[5] to_indices
@ .\indices.jl:345 [inlined]
[6] getindex
@ .\abstractarray.jl:1294 [inlined]
[7] get_attributes(data::Vector{UInt8}, i::Int64, j::Int64)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\raw.jl:126
[8] attributes(o::XML.Raw)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\raw.jl:164
[9] getproperty
@ C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:55 [inlined]
[10] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:126
[11] iterate(g::Base.Generator{Vector{LazyNode}, Type{Node}}, s::Int64)
@ Base .\generator.jl:47
[12] collect_to!(dest::Vector{Node}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, offs::Int64, st::Int64)
@ Base .\array.jl:840
[13] collect_to_with_first!(dest::Vector{Node}, v1::Node, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, st::Int64)
@ Base .\array.jl:818
[14] _collect(c::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base .\array.jl:812
[15] collect_similar(cont::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\array.jl:711
[16] map(f::Type, A::Vector{LazyNode})
@ Base .\abstractarray.jl:3261
[17] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:128
[18] iterate(g::Base.Generator{Vector{LazyNode}, Type{Node}}, s::Int64)
@ Base .\generator.jl:47
[19] collect_to!(dest::Vector{Node}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, offs::Int64, st::Int64)
@ Base .\array.jl:840
[20] collect_to_with_first!(dest::Vector{Node}, v1::Node, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, st::Int64)
@ Base .\array.jl:818
[21] _collect(c::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base .\array.jl:812
[22] collect_similar(cont::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\array.jl:711
[23] map(f::Type, A::Vector{LazyNode})
@ Base .\abstractarray.jl:3261
[24] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:128
[25] iterate(g::Base.Generator{Vector{LazyNode}, Type{Node}}, s::Int64)
@ Base .\generator.jl:47
[26] collect_to!(dest::Vector{Node}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, offs::Int64, st::Int64)
@ Base .\array.jl:840
[27] collect_to_with_first!(dest::Vector{Node}, v1::Node, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, st::Int64)
@ Base .\array.jl:818
[28] _collect(c::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base .\array.jl:812
[29] collect_similar(cont::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\array.jl:711
[30] map(f::Type, A::Vector{LazyNode})
@ Base .\abstractarray.jl:3261
[31] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:128
[32] iterate(::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\generator.jl:47
[33] _collect(c::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base .\array.jl:802
[34] collect_similar(cont::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\array.jl:711
[35] map(f::Type, A::Vector{LazyNode})
@ Base .\abstractarray.jl:3261
[36] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:128
[37] iterate(g::Base.Generator{Vector{LazyNode}, Type{Node}}, s::Int64)
@ Base .\generator.jl:47
[38] collect_to!(dest::Vector{Node}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, offs::Int64, st::Int64)
@ Base .\array.jl:840
[39] collect_to_with_first!(dest::Vector{Node}, v1::Node, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, st::Int64)
@ Base .\array.jl:818
[40] _collect(c::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base .\array.jl:812
[41] collect_similar(cont::Vector{LazyNode}, itr::Base.Generator{Vector{LazyNode}, Type{Node}})
@ Base .\array.jl:711
[42] map(f::Type, A::Vector{LazyNode})
@ Base .\abstractarray.jl:3261
[43] Node(node::LazyNode)
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:128
[44] Node
@ C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:121 [inlined]
[45] read(filename::String, #unused#::Type{Node})
@ XML C:\Users\dfsma.julia\packages\XML\SrduN\src\XML.jl:132
[46] top-level scope
@ REPL[75]:1

parisge.zip

Implement the Tables.jl interface

The Shapefile.jl and GeoJSON.jl packages implement the Tables.jl interface, so it would be interesting if KML.jl does as well.
Would this be in the development plans for KML.jl?
Thanks in advance to the maintainers for their attention.

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Lines not working

Hello, I've been using this package since 0.1.0 (thanks for the development!) but since 0.2.0 I can't define lines that are readable by Google Earth.

An example of an output is:

<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Folder>
      <name>Barreras</name>
      <Placemark>
        <name>Barrera-LT01</name>
        <LineString>
          <coordinates>-70.78367300816775, -34.415271246424886
-70.78358070922826, -34.415403734874154</coordinates>
        </LineString>
      </Placemark>
    </Folder>
  </Document>
</kml>

I suppose the coordinates tags and lack of indentation could be causing the problem. The code for generating this is working as expected in 0.1.1 but not in 0.2.x (none of them).

Could you check, please? The code hasn't problems working but points, but there are issues with 2 or more coordinates in an element. If you need more information, tell me please.

Thanks in advance

Felipe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.