Select to view content in your preferred language

Get the right map extent

356
1
09-06-2022 04:29 AM
Daniel4
Emerging Contributor

Hey, I am building an Addin to AGP that gets data from an Oracle database and displaying on a map. The user can from a combobox select the layer thay want to see.

When I am creating a new projekt i pro and select a layer from the combobox, the map are zooming out to this:

Daniel4_0-1662463345180.png

QueryDescription queryDescription = oracleDatabase.GetQueryDescription(SQLquery, this.tableName);
queryDescription.SetObjectIDFields("OBJECTID");
queryDescription.SetShapeType(geomType);
SpatialReference spatialReference = MapView.Active.Map.SpatialReference;

Table table = oracleDatabase.OpenTable(queryDescription);
                   

                    
FeatureClass featureClass = oracleDatabase.OpenTable(queryDescription) as FeatureClass;
if (featureClass.GetCount() != 0)
{
FeatureLayer featureLayer = LayerFactory.Instance.CreateFeatureLayer(featureClass, map, layerName: tableName);
} else
{
// Display a message
}

 

How can I make it zoom correctly?

0 Kudos
1 Reply
CharlesMacleod
Esri Regular Contributor

I believe query layers have a data connection that contains an extent property that u can set via the CIM. In the case of a query layer, it may be null by default as the software probably wants to avoid doing a full table scan to calculate it ....thus, I assume the layer extent gets defaulted to something v large - probably the map's default extent or similar....

As a workaround, perhaps u could create the layer with visibility off then set the extent u desire, turning visibility on at the same time. "Zoom to Layer", I believe, will also honor any extent u set.

Here's a snippet that does something along those lines that u can play with:

//elsewhere - compute extent envelope as needed...
 var extent = EnvelopeBuilderEx.CreateEnvelope(x_min, y_min, x_max, y_max);

 var feat_layer = LayerFactory.Instance.CreateLayer(....) as FeatureLayer;
 QueuedTask.Run(() => {
   //Get the CIM Definition
   var def = feat_layer.GetDefinition() as CIMFeatureLayer;
   def.Visibility = true; //make the layer visible
   var sql_conn = def.FeatureTable.DataConnection as CIMSqlQueryDataConnection;
   if (sql_conn != null) {
     //Set the extent - honored by "Zoom to Layer", etc.
     sql_conn.Extent = extent;
   }
   feat_layer.SetDefinition(def);//commit the changes
 });

 

Zoom to Layer should use the extent, also:

zoom_to_layer.jpg

 

Additionally, "Recalculate Feature Class Extent" gp tool provides an option to  store the calc'd extent as part of an unregistered table (aka query layer) at the db level.  The next time you add a query layer, it should read the extent off the table's metadata without recalc or need to set the extent programmatically.

gp_recalc_extent.jpg

 

0 Kudos