2

So I have thousands of coordinates that i need to plot and one of the best method i know is by saving the coordinates as .txt in asset folder and then pull it by using

val json_string = application.assets.open(file_name).bufferedReader().use{it.readText()}

But when the file is pulled, it read the file as a string, despite the file is saved in array format such as follow:

"97.951476367000055,3.155017079000061","97.951800004000063,3.154406602000051","97.949325673000033,3.156145963000029","97.948721732000081,3.15657051200003","97.948646156000052,3.156623638000042","97.947494978000066,3.15786896100002","97.94724135000007,3.158143331000076","97.947000011000057,3.158404405000056","97.946790649000036,3.158630887000072","97.946272953000062,3.15919092200005","97.945702608000033,3.15980791000004","97.945553326000038,3.159969400000023","97.944601930000033,3.160998592000055","97.944409762000078,3.161206474000039".... 

I want to read the asset in an array format, for example, it is divided by comma and grouped by quotes, for example:

  • "97.951476367000055,3.155017079000061"
  • "97.951800004000063,3.154406602000051"
  • "97.949325673000033,3.156145963000029"
  • and so on...

How can I read this .txt file asset as string array or ArrayList< String > format in Kotlin?

Thanks!

3
  • I would think a far better way would be to store it as doubles (write them to the file as bytes). About 9 times smaller file that would be much faster to read and write. But it depends on the precision you need. Commented Oct 13, 2023 at 13:36
  • Use split() to divide the string into substrings based on the delimiter. Commented Oct 13, 2023 at 13:39
  • despite the file is saved in array format such as follow: That is no array format. It is just one text line with comma separated values. You better make a csv/txt file with lines. A line for every location. And it is no json string either. Commented Oct 13, 2023 at 13:40

2 Answers 2

1

Create a model like below

data class Coordinate(val latitude: String, val longitude: String) 

// Function to read coordinates from the asset file and convert to ArrayList

fun readCoordinatesFromAsset(context: Context, assetFileName: String): ArrayList<String> { val gson = Gson() val arrayList = ArrayList<String>() try { context.assets.open(assetFileName).use { inputStream -> val reader = InputStreamReader(inputStream) val coordinates = gson.fromJson(reader, ArrayList::class.java) // Convert the ArrayList of Coordinate objects to an ArrayList of strings for (coordinate in coordinates) { arrayList.add(coordinate.toString()) } } } catch (e: Exception) { e.printStackTrace() } return arrayList } 

Your text file should be like the below format

coordinate.txt

[ "97.951476367000055,3.155017079000061", "97.951800004000063,3.154406602000051", "97.949325673000033,3.156145963000029", "97.948721732000081,3.15657051200003", "97.948646156000052,3.156623638000042", "97.947494978000066,3.15786896100002", "97.94724135000007,3.158143331000076", "97.947000011000057,3.158404405000056", "97.946790649000036,3.158630887000072", "97.946272953000062,3.15919092200005", "97.945702608000033,3.15980791000004", "97.945553326000038,3.159969400000023", "97.944601930000033,3.160998592000055", "97.944409762000078,3.161206474000039" ] 

You can call the method as

val resulArray: ArrayList<String> =readCoordinatesFromAsset(this,"coordinate.txt") 
Sign up to request clarification or add additional context in comments.

1 Comment

It works smoothly! Great!
0

You can try something like this, see the code below.

https://pl.kotl.in/NQQdTW6m4?theme=darcula

Code for the problem

Click on the link above, to test and run the code, also you can define your own model class, instead of the Pair() which I have use to demonstrate in this case. Feel free to improve it!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.