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:
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?
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:
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.