I am developing an Android application using the ArcGIS Maps SDK for Kotlin (version 200.x) and I'm trying to load and display a GeoJSON file from local device storage.
I found the FeatureCollection.fromJsonOrNull to try to load the file.But it cannot be analyzed correctly.
fun loadGeoJSONFromContentUri(context:Context, uri: Uri, fileExtension: String) {
try {
val tempFile = File(context.cacheDir, "temp_${System.currentTimeMillis()}.$fileExtension")
context.contentResolver.openInputStream(uri)?.use { inputStream ->
FileOutputStream(tempFile).use { outputStream ->
inputStream.copyTo(outputStream)
}
}
val geoJsonString = tempFile.readText(Charsets.UTF_8)
val featureCollection = FeatureCollection.fromJsonOrNull(geoJsonString)
if(featureCollection == null){
tempFile.delete()
return
}
featureCollection.tables.forEach {
it.layer?.let { layer ->
arcGISMap.operationalLayers.add(layer)
}
}
tempFile.delete()
} catch (e: Exception) {
Toast.makeText(context, e.message ?: "GeoJSON file load faild", Toast.LENGTH_SHORT).show()
Log.e(TAG, " ${e.printStackTrace()}")
}
}
Am I using the wrong method, or is there another recommended way to load a GeoJSON file?
ArcGIS Maps SDK for Kotlin (version 200.6)
Hi @LiqiangZhu ,
Am I using the wrong method, or is there another recommended way to load a GeoJSON file?
The ArcGIS Maps SDKs don't support GeoJSON yet. FeatureCollection.fromJsonOrNull can only parse json that conforms with the webmap specification.
You could parse the GeoJSON file by yourself to deserialize geometries and attributes. Once you have those you can create a Feature via FeatureCollectionTable.createFeature and adding the new feature to the feature table via FeatureCollectionTable.addFeature. See FeatureCollectionTable for how to create one.
This sample shows how to parse geometries and attributes from json using Kotlin json serialization. Relevant parsing code here.