@dunguel/expo-geo-parser 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -60,7 +60,9 @@ class KMLParser(
|
|
|
60
60
|
companion object {
|
|
61
61
|
fun parse(input: InputStream, sourceType: String, onFeatures: (List<Map<String, Any?>>, Boolean) -> Unit): Map<String, Any?> {
|
|
62
62
|
val handler = KMLParser(onFeatures = onFeatures)
|
|
63
|
-
|
|
63
|
+
// Some KML files in the wild contain prefixed tags without proper xmlns declarations.
|
|
64
|
+
// Parsing without namespace enforcement keeps the stream parser resilient on Android.
|
|
65
|
+
val factory = SAXParserFactory.newInstance().apply { isNamespaceAware = false }
|
|
64
66
|
try {
|
|
65
67
|
factory.newSAXParser().parse(input, handler)
|
|
66
68
|
} catch (e: Exception) {
|
|
@@ -77,14 +79,14 @@ class KMLParser(
|
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
override fun startElement(uri: String?, localName: String?, qName: String?, attrs: Attributes?) {
|
|
80
|
-
val name =
|
|
82
|
+
val name = resolveElementName(localName, qName)
|
|
81
83
|
elementStack.add(name)
|
|
82
84
|
textBuffer.clear()
|
|
83
85
|
|
|
84
86
|
when (name) {
|
|
85
87
|
"Document" -> documentDepth++
|
|
86
|
-
"Style" -> { currentStyleId = attrs
|
|
87
|
-
"StyleMap" -> { inStyleMap = true; currentStyleMapId = attrs
|
|
88
|
+
"Style" -> { currentStyleId = getAttribute(attrs, "id"); buildingStyle = StyleInfo() }
|
|
89
|
+
"StyleMap" -> { inStyleMap = true; currentStyleMapId = getAttribute(attrs, "id") }
|
|
88
90
|
"Pair" -> if (inStyleMap) { inPair = true; currentPairKey = ""; currentPairStyleUrl = "" }
|
|
89
91
|
"LineStyle" -> inLineStyle = true
|
|
90
92
|
"PolyStyle" -> inPolyStyle = true
|
|
@@ -92,7 +94,7 @@ class KMLParser(
|
|
|
92
94
|
"Icon" -> if (inIconStyle) inIconHref = true
|
|
93
95
|
"Placemark" -> {
|
|
94
96
|
inPlacemark = true
|
|
95
|
-
currentFeatureId = attrs
|
|
97
|
+
currentFeatureId = getAttribute(attrs, "id")
|
|
96
98
|
currentFeatureName = ""
|
|
97
99
|
currentFeatureDescription = ""
|
|
98
100
|
currentFeatureStyleUrl = ""
|
|
@@ -103,7 +105,7 @@ class KMLParser(
|
|
|
103
105
|
multiGeometries = mutableListOf()
|
|
104
106
|
}
|
|
105
107
|
"ExtendedData" -> if (inPlacemark) inExtendedData = true
|
|
106
|
-
"Data", "SimpleData" -> if (inPlacemark && inExtendedData) currentExtendedDataName = attrs
|
|
108
|
+
"Data", "SimpleData" -> if (inPlacemark && inExtendedData) currentExtendedDataName = getAttribute(attrs, "name") ?: ""
|
|
107
109
|
"Point" -> { currentGeometryType = "Point"; pointCoord = listOf() }
|
|
108
110
|
"LineString" -> { currentGeometryType = "LineString"; lineCoords = listOf() }
|
|
109
111
|
"LinearRing" -> currentRing = listOf()
|
|
@@ -123,7 +125,7 @@ class KMLParser(
|
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
override fun endElement(uri: String?, localName: String?, qName: String?) {
|
|
126
|
-
val name =
|
|
128
|
+
val name = resolveElementName(localName, qName)
|
|
127
129
|
val text = textBuffer.toString().trim()
|
|
128
130
|
val parent = elementStack.dropLast(1).lastOrNull() ?: ""
|
|
129
131
|
textBuffer.clear()
|
|
@@ -217,6 +219,25 @@ class KMLParser(
|
|
|
217
219
|
return if (idx >= 0) name.substring(idx + 1) else name
|
|
218
220
|
}
|
|
219
221
|
|
|
222
|
+
private fun resolveElementName(localName: String?, qName: String?): String {
|
|
223
|
+
val raw = localName?.takeIf { it.isNotEmpty() } ?: qName ?: ""
|
|
224
|
+
return stripped(raw)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private fun getAttribute(attrs: Attributes?, name: String): String? {
|
|
228
|
+
if (attrs == null) return null
|
|
229
|
+
attrs.getValue(name)?.let { return it }
|
|
230
|
+
|
|
231
|
+
for (index in 0 until attrs.length) {
|
|
232
|
+
val local = attrs.getLocalName(index)
|
|
233
|
+
if (local == name) return attrs.getValue(index)
|
|
234
|
+
|
|
235
|
+
val qName = attrs.getQName(index)
|
|
236
|
+
if (stripped(qName) == name) return attrs.getValue(index)
|
|
237
|
+
}
|
|
238
|
+
return null
|
|
239
|
+
}
|
|
240
|
+
|
|
220
241
|
private fun isCollectionContainer(name: String): Boolean =
|
|
221
242
|
name == "Document" || name == "Folder"
|
|
222
243
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dunguel/expo-geo-parser",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "A high-performance Expo native module for parsing geospatial files like KML, KMZ, and other formats, extracting polygons, markers, and spatial data efficiently on iOS and Android.",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|