Query a feature

960
11
07-23-2023 05:20 AM
majdkassab
New Contributor

Hello, How can I Query a feature in kotlin ? If there is a complete or a good guide that might help.



0 Kudos
11 Replies
DanPatterson
MVP Esteemed Contributor

did you see the esri documentation?

ArcGIS Maps SDK for Kotlin | ArcGIS Developers


... sort of retired...
0 Kudos
majdkassab
New Contributor

The available documentation lacks sufficient explanatory content and examples on how to query the feature effectively, which has made it hard to understand and implement the desired functionality.

0 Kudos
MikeWilburn
Esri Contributor

Hi @majdkassab, can you tell us a little more about what you're trying to do?

Initially I'd suggest reading through our guide on this topic, but it sounds like you've found that to be lacking the explanation you were hoping to see. With a little more detail, I'm sure we can help. 

0 Kudos
majdkassab
New Contributor

Hello @MikeWilburn ,I'm trying to just query a feature where I get its result
back but  have no way/idea on how to get the result back once it finishes. I found this link 
https://community.esri.com/t5/arcgis-%E9%96%8B%E7%99%BA%E8%80%85%E3%82%B3%E3%83%9F%E3%83%A5%E3%83%8B...
but as shown in the screenshot the 
Unresolved reference: queryFeaturesAsync 

this is the relevant code

var searchForPolygon: (String) -> Unit = { searchString ->

var query = QueryParameters()
query.whereClause = createstr(searchString)
Log.d(TAG, query.whereClause.toString())

// Perform the query on the hosted polygon data
val future = serviceFeatureTable!!.queryFeaturesAsync(query)

// Use the SAM conversion to handle the query result
future.addDoneListener {
try {
// Get the entire result of the query
val result = future.get()

// Check the result
if (result.iterator().hasNext()) {
// Get the first result and zoom to the corresponding feature
val feature = result.iterator().next()
val envelope = feature.geometry.extent



} else {
Toast.makeText(this@MainActivity, "No polygon found for this search: " + searchString, Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(this@MainActivity, "Failed to search for features: " + searchString + ". Error=" + e.message, Toast.LENGTH_SHORT).show()
Log.e(TAG, "Feature search failed for: " + searchString + ". Error=" + e.message)
}
}
}
fun createstr (searchString: String) : String {
var reString: String? = null
val ku = searchString.split("".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
if (ku.size > 1) {

reString = "upper(CSS_NAME) LIKE '" + ku[0] + "' and (MOJI) LIKE '%" + ku[1] + "%'"
} else {
reString = "upper(MOJI) LIKE '%" + ku[0] + "%'"
}
Log.d(TAG, "SQL String="+ reString)
return reString
}

the documentation is quite lacking as there is no code on how use the functions. 

Thank you

0 Kudos
MikeWilburn
Esri Contributor

The code you've shared appears to be using the 100.x Java-based Android SDK. Unfortunately, it is not compatible with our 200.x Kotlin SDK.

In particular, the unresolved reference you're encountering is due to the fact that there is no such function queryFeaturesAsync. You'll need to instead use queryFeatures.

 

0 Kudos
majdkassab
New Contributor

Based on the link its written in kotlin. I'm working in kotlin.
Can you please expand on the100.x java vs 200.x kotlin?
thank you 

 

0 Kudos
MikeWilburn
Esri Contributor

Apologies for the confusion. We have two separate APIs for writing native Android apps -- an older 100.x Runtime SDK for Android and our newer 200.x Maps SDK for Kotlin.

See this page in our guide about the differences between the two and how to migrate:
https://developers.arcgis.com/kotlin/reference/migrate-from-100-x-to-200-x/

While the code you shared is written in Kotlin, it appears to leverage our Java-based Runtime SDK for Android. @SorenRoth's post below handles many of the considerations stated within the migration topic to help your code work with the Maps SDK for Kotlin.

Otherwise, if you have a question on using the Runtime/Android SDK, consider posting it on this board:
https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/bd-p/arcgis-runtime-sdk-for-a...

0 Kudos
majdkassab
New Contributor

I see thank you. After trying with his solution it didn't work as of right now, Ill give it another shot and what possible changes are needed.

0 Kudos
SorenRoth
Esri Contributor

As @MikeWilburn suggested, the 200.x Kotlin SDK provides concurrency through coroutines . This sample is quite similar to your use case, but instead of identifying layers, you would query features. An adaptation of your code to use coroutines would look like below (please note the suspend keyword in the function signature)

var searchForPolygon: suspend (String) -> Unit = { searchString ->
         var query = QueryParameters()
        query.whereClause = createSearchStr()
         // Perform the query on the hosted polygon data
        serviceFeatureTable!!.queryFeatures(query)
            .onSuccess {
                if (it.iterator().hasNext()) {
                    // Get the first result and zoom to the corresponding feature
                    val feature = it.iterator().next()
                    val envelope = feature.geometry?.extent
                } else {
                    withContext(Dispatchers.Main) {
                        Toast.makeText(
                            this@MainActivity,
                            "No polygon found for this search: " + searchString,
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }
            .onFailure {
                Log.e(TAG, "Feature search failed for: " + searchString + ". Error=" + e.message)
                withContext(Dispatchers.Main) {
                    Toast.makeText(
                        this@MainActivity,
                        "Failed to search for features: " + searchString + ". Error=" + e.message,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

 

0 Kudos