How to perform SQL query based search on a layer of regions in MapXtreme

UPDATED: April 10, 2017

One of the most common tasks in mapping applications is to search for features that meet certain criteria. Once you have the features you are interested in, you can carry out further analysis, such as thematic mapping.

Sample code for SQL query based search:
C# Code: 
	// create a Table object that targets a TAB file
	MapInfo.Engine.ISession iSession = MapInfo.Engine.Session.Current;
	MapInfo.Data.Catalog catalog = iSession.Catalog;
	MapInfo.Data.Table table = 
	catalog.OpenTable("C:\\Program Files\\MapInfo\\MapXtreme\\6.5\\Samples\\Data\\usa.TAB","usa");

	// create a FeatureLayer from the Table object
	MapInfo.Mapping.FeatureLayer featureLayer = new FeatureLayer(table);

	// create a SearchInfo
	// where clause constraints
	IQueryFilter iQueryFilter = new SqlExpressionFilter("state = 'OH' or state = 'NY'"); 
	QueryDefinition queryDefinition = new QueryDefinition(iQueryFilter);
	// columns to return 
	queryDefinition.Columns = new String [] {"state_name, state, obj"}; // obj == geometry
	MapInfo.Data.SearchInfo searchInfo = new SearchInfo(queryDefinition);

	// use the catalog to perform the search
	IResultSetFeatureCollection iResultSetFeatureCollection =

	// determine the columns within the IResultSetFeatureCollection return table of records
	ArrayList arrayList = new ArrayList();
	MapInfo.Data.Columns columns = iResultSetFeatureCollection.Table.TableInfo.Columns;
	foreach(MapInfo.Data.Column column in columns)
		String columnNameAlias = column.Alias;

	// for each feature
	MapInfo.Geometry.FeatureGeometry featureGeometry = null;
	String stateName = null;
	String state = null;
	foreach(MapInfo.Data.Feature feature in iResultSetFeatureCollection)
		// iterate through its columns
		foreach(Column column in feature.Columns)
			// get the name column's alias name
			String columnAlias = column.Alias;
			// if state_name
			if (columnAlias.ToLower().Equals("state_name"))
				stateName = (String)feature[columnAlias];
			// else if state abbreviation
			else if (columnAlias.ToLower().Equals("state"))
				state = (String)feature[columnAlias];
			// else if geometry
			else if (columnAlias.ToLower().Equals("obj"))
				featureGeometry = (FeatureGeometry)feature[columnAlias];
catch(Exception exception)
	Console.WriteLine("EXCEPTION: ["+exception.Message.ToString()+"]");


Environment Details

Product Feature: General



  • No Downloads