Skip to content

Commit 8896c5c

Browse files
committed
Version 1.0
1 parent a6f92b4 commit 8896c5c

File tree

5 files changed

+398
-0
lines changed

5 files changed

+398
-0
lines changed

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
# kmlExtractor
2+
23
KML data extractor written in Go
4+
5+
## How to use
6+
7+
```golang
8+
package main
9+
```
10+
11+
## References
12+
13+
[KML Reference](http://dh.obdurodon.org/kml/kml-tutorial.xhtml)

Diff for: kmlextractor.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License.
16+
*/
17+
18+
package kmlextractor
19+
20+
import (
21+
"archive/zip"
22+
"bufio"
23+
"encoding/xml"
24+
"github.com/kevinborras/kmlextractor/model"
25+
"os"
26+
"strings"
27+
)
28+
29+
//checkType checks if it is possible to unzip the .kml and obtain more information
30+
func checkZipped(document *os.File) (bool, error) {
31+
32+
var err error
33+
b1 := make([]byte, 2)
34+
_, err = document.Read(b1)
35+
if err != nil {
36+
return false, err
37+
}
38+
if string(b1) == "PK" {
39+
return true, err
40+
}
41+
return false, err
42+
43+
}
44+
45+
//GetContent func returns all the information insede the .kml file
46+
func GetContent(document *os.File) (kmlInfo model.KML, err error) {
47+
48+
flag, err := checkZipped(document)
49+
if err != nil {
50+
return kmlInfo, err
51+
}
52+
if flag {
53+
document.Close()
54+
//check for files
55+
oriName := document.Name()
56+
dot := strings.LastIndex(oriName, ".")
57+
zipName := oriName[:dot] + ".zip"
58+
err := os.Rename(oriName, zipName)
59+
if err != nil {
60+
return kmlInfo, err
61+
}
62+
read, err := zip.OpenReader(zipName)
63+
if err != nil {
64+
os.Rename(zipName, oriName)
65+
return kmlInfo, err
66+
}
67+
xmlFile := ""
68+
for _, file := range read.File {
69+
if file.Name == "doc.kml" {
70+
rc, err := file.Open()
71+
if err != nil {
72+
return kmlInfo, err
73+
}
74+
scanner := bufio.NewScanner(rc)
75+
for scanner.Scan() {
76+
xmlFile += scanner.Text()
77+
}
78+
}
79+
}
80+
if err := xml.Unmarshal([]byte(xmlFile), &kmlInfo); err != nil {
81+
os.Rename(zipName, oriName)
82+
return kmlInfo, err
83+
}
84+
read.Close()
85+
os.Rename(zipName, oriName)
86+
87+
} else {
88+
fStats, err := document.Stat()
89+
if err != nil {
90+
return kmlInfo, err
91+
}
92+
//Byte slice of document size
93+
docBytes := make([]byte, fStats.Size())
94+
_, err = document.Read(docBytes)
95+
if err := xml.Unmarshal(docBytes, &kmlInfo); err != nil {
96+
return kmlInfo, err
97+
}
98+
document.Close()
99+
}
100+
101+
return kmlInfo, err
102+
}

Diff for: model/kmlstruct.go

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License.
16+
*/
17+
18+
package model
19+
20+
//KML struct
21+
type KML struct {
22+
Doc Document `xml:"Document"`
23+
Placemarks []PlaceMark `xml:"Placemark"`
24+
F Folder `xml:"Folder"`
25+
NetLink NetworkLink `xml:"NetworkLink"`
26+
}
27+
28+
//Document struct
29+
type Document struct {
30+
Name string `xml:"name"`
31+
StyleM StyleMap `xml:"StyleMap"`
32+
Style []Style `xml:"Style"`
33+
F []Folder `xml:"Folder"`
34+
Lookat LookAt `xml:"LookAt"`
35+
NetLink NetworkLink `xml:"NetworkLink"`
36+
}
37+
38+
//NetworkLink struct
39+
type NetworkLink struct {
40+
Name string `xml:"name"`
41+
Visibility int `xml:"visibility"`
42+
Open int `xml:"open"`
43+
Lookat LookAt `xml:"LookAt"`
44+
FlyToView string `xml:"flyToView"`
45+
URLInfo URL `xml:"Url"`
46+
}
47+
48+
//LookAt struct
49+
type LookAt struct {
50+
Longitude string `xml:"longitude"`
51+
Latitude string `xml:"latitude"`
52+
Altitude string `xml:"altitude"`
53+
Range string `xml:"range"`
54+
Tilt string `xml:"tilt"`
55+
Heading string `xml:"heading"`
56+
AltitudeMode string `xml:"altitudeMode"`
57+
}
58+
59+
//URL struct
60+
type URL struct {
61+
Href string `xml:"href"`
62+
RefreshInterval string `xml:"refreshInterval"`
63+
ViewRefreshMode string `xml:"viewRefreshMode"`
64+
ViewRefreshTime string `xml:"viewRefreshTime"`
65+
ViewBoundScale string `xml:"viewBoundScale"`
66+
}
67+
68+
//StyleMap struct
69+
type StyleMap struct {
70+
Pairs []Pair `xml:"Pair"`
71+
}
72+
73+
//Pair struct
74+
type Pair struct {
75+
Key string `xml:"key"`
76+
StyleURL string `xml:"styleUrl"`
77+
}
78+
79+
//Style struct
80+
type Style struct {
81+
IconS IconStyle `xml:"IconStyle"`
82+
LineS LineStyle `xml:"LineStyle"`
83+
ListS ListStyle `xml:"ListStyle"`
84+
}
85+
86+
//IconStyle struct customizes the pinpoints
87+
type IconStyle struct {
88+
Scale string `xml:"scale"`
89+
Ico Icon `xml:"Icon"`
90+
HS HotSpot `xml:"hotSpot"`
91+
}
92+
93+
//Icon struct contains the link to the icon
94+
type Icon struct {
95+
Href string `xml:"href"`
96+
}
97+
98+
//HotSpot struct
99+
type HotSpot struct {
100+
X string `xml:"x,attr"`
101+
Y string `xml:"y,attr"`
102+
Xunits string `xml:"xunits,attr"`
103+
Yunits string `xml:"yunits,attr"`
104+
}
105+
106+
//LineStyle struct styles the strokes that join two or more geographical points
107+
type LineStyle struct {
108+
Color string `xml:"color"`
109+
Width string `xml:"width"`
110+
}
111+
112+
//ListStyle struct
113+
type ListStyle struct {
114+
ListItemType string `xml:"listItemType"`
115+
ItemIco ItemIcon `xml:"ItemIcon"`
116+
BgColor string `xml:"bgColor"`
117+
MaxSnippetLines string `xml:"maxSnippetLines"`
118+
}
119+
120+
//ItemIcon struct
121+
type ItemIcon struct {
122+
State string `xml:"state"`
123+
Href string `xml:"href"`
124+
}
125+
126+
//Folder struct
127+
type Folder struct {
128+
Name string `xml:"name"`
129+
Visibility string `xml:"description"`
130+
Open int `xml:"open"`
131+
F []Folder `xml:"Folder"`
132+
Lookat LookAt `xml:"LookAt"`
133+
Placemarks []PlaceMark `xml:"Placemark"`
134+
PhotoOverlayList []PhotoOverlay `xml:"PhotoOverlay"`
135+
}
136+
137+
//PlaceMark struct pinpoints a location through reference to the Earth’s surface
138+
type PlaceMark struct {
139+
Name string `xml:"name"`
140+
Description string `xml:"description"`
141+
P Point `xml:"Point"`
142+
StyleURL string `xml:"styleUrl"`
143+
LineS LineString `xml:"LineString"`
144+
}
145+
146+
//LineString struct identifies at least two points to connect
147+
type LineString struct {
148+
Tssellate string `xml:"tessellate"`
149+
AltitudeMode string `xml:"altitudeMode"`
150+
Coordinates string `xml:"coordinates"`
151+
}
152+
153+
//PhotoOverlay struct
154+
type PhotoOverlay struct {
155+
Name string `xml:"name"`
156+
Cam Camera `xml:"Camera"`
157+
Style Style `xml:"Style"`
158+
Ico Icon `xml:"Icon"`
159+
Rotation string `xml:"rotation"`
160+
VVolume ViewVolume `xml:"ViewVolume"`
161+
P Point `xml:"Point"`
162+
}
163+
164+
//Camera struct
165+
type Camera struct {
166+
Longitude string `xml:"longitude"`
167+
Latitude string `xml:"latitude"`
168+
Altitude string `xml:"altitude"`
169+
Heading string `xml:"heading"`
170+
Tilt string `xml:"tilt"`
171+
Rll string `xml:"roll"`
172+
AltitudeMode string `xml:"altitudeMode"`
173+
}
174+
175+
//ViewVolume struct
176+
type ViewVolume struct {
177+
LeftFov string `xml:"leftFov"`
178+
RightFov string `xml:"rightFov"`
179+
BottomFov string `xml:"bottomFov"`
180+
TopFov string `xml:"topFov"`
181+
Near string `xml:"near"`
182+
}
183+
184+
//Point struct contains geographical information
185+
type Point struct {
186+
AltitudeMode string `xml:"altitudeMode"`
187+
Coordinates string `xml:"coordinates"`
188+
}

Diff for: samples/doc.kml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<kml xmlns="http://earth.google.com/kml/2.1">
3+
<NetworkLink>
4+
<name>Real-time Earthquakes</name>
5+
<open>1</open>
6+
<LookAt>
7+
<longitude>-104.1025</longitude>
8+
<latitude>50.4913</latitude>
9+
<altitude>0</altitude>
10+
<range>8384245</range>
11+
<tilt>0</tilt>
12+
<heading>-0.2564</heading>
13+
</LookAt>
14+
<flyToView>1</flyToView>
15+
<Url>
16+
<href>http://services.google.com/earth/kmz/realtime_earthquakes.kmz</href>
17+
</Url>
18+
</NetworkLink>
19+
</kml>

0 commit comments

Comments
 (0)