Maptitude GISDK Help |
This example opens a connection to the GISDK environment, geocodes a table of street addresses, produces a geographic layer that can be added to a map, and closes the connection to the GISDK environment:
using System;
using System.Collections.Generic;
using System.Text;
using CaliperForm;
///
/// Geocode a table of street addresses and produce a geographic layer that can be added to a map.
///
static void Geocode_Table() {
CaliperForm.Connection Conn = new CaliperForm.Connection
{ MappingServer = mapping_product };
Boolean opened = false;
try {
opened = Conn.Open(log_file);
if (opened) {
// You must declare dk as "dynamic" or the compiler will throw an error
dynamic dk = Conn.Gisdk;
string tutorial_folder = dk.Macro("G30 Tutorial Folder") as string;
string excel_file = tutorial_folder + "Sample Customers United States.xls";
if (!System.IO.File.Exists(excel_file)) {
Console.Out.WriteLine("Cannot find tutorial example file: " + excel_file);
return;
}
string sheet_name = "My Customers$";
string binary_table = tutorial_folder + "My Example Customers.bin";
string exported_table = dk.Macro("G30 export an Excel sheet", excel_file,
sheet_name, binary_table) as string;
if (exported_table != null) {
Console.Out.WriteLine("Exported Excel file: " + excel_file + " to: " +
exported_table);
dynamic geo = dk.CreateObject("Data.Geocoder");
geo.SetRegion();
string region_name = geo.GetRegionName();
// Open the exported table
string table_name = dk.OpenTable("customers", "ffb", new Object[]
{ exported_table , null });
int num_records = dk.GetRecordCount(table_name, null);
Console.Out.WriteLine("Locating " + Convert.ToString(num_records) +
" records in table " + table_name + " in region " + region_name + "...");
// Get the input field specifications
var id_field = dk.GetFieldFullSpec(table_name, "Customer ID") as string;
var address_field = dk.GetFieldFullSpec(table_name, "Street Address") as string;
var postal_field = dk.GetFieldFullSpec(table_name, "ZIP Code") as string;
var city_field = dk.GetFieldFullSpec(table_name,"City") as string;
var state_field = dk.GetFieldFullSpec(table_name, "State") as string;
string output_layer_db = tutorial_folder + "Located Customers.dbd";
var opts = new OptionsArray();
opts["new_layer_name"] = "Located Customers";
opts["out_db"] = output_layer_db;
// First, locate records by street address and postal code,
// and save the result to the layer db "Located Customers.dbd"
// input_fields = { address_field , address_field_2 , postal_code_field ,
// city_field_or_value , state_field_or_value }
object[] input_fields = new object[] { address_field, null, postal_field };
Dictionary<String, Object> result = Cast.ToDictionary(geo.LocateView("ADDRESS",
table_name + "|", id_field, input_fields, opts));
// if success:
// result = {"Message":"OK","NotFoundSet":"Address Not Found",
// "LayerName":"Located Customers","NumRecords":4800, "NumLocated":...,
// "OutputLayer":...,"GeocodingLayer":...}
// else:
// result = {"Message":"Error Message","Error":1}
if ((result != null) && (result["LayerName"] as string != null) &&
(result["NotFoundSet"] as string != null)) {
// Then, locate the records not found by postal code
string not_found_set = result["NotFoundSet"] as string;
string layer_name = result["LayerName"] as string;
int num_not_found = dk.GetRecordCount(layer_name, not_found_set);
string street_layer_db = result["GeocodingLayer"] as string;
Console.Out.WriteLine(Convert.ToString(num_not_found) +
" records not found by street address using street layer " + street_layer_db);
id_field = dk.GetFieldFullSpec(layer_name, "ID") as string;
address_field = dk.GetFieldFullSpec(layer_name, "Street Address") as string;
postal_field = dk.GetFieldFullSpec(layer_name, "ZIP Code") as string;
city_field = dk.GetFieldFullSpec(layer_name, "City") as string;
state_field = dk.GetFieldFullSpec(layer_name, "State") as string;
input_fields = new object[] { postal_field };
result = Cast.ToDictionary(geo.LocateView("POSTAL_CODE", layer_name +
"|" + not_found_set, id_field, input_fields, null));
var located_by_postal_code = (int) result["NumLocated"];
string postal_layer_db = result["GeocodingLayer"] as string;
Console.Out.WriteLine(Convert.ToString(located_by_postal_code)
+ " records located by postal code using postal code layer " + postal_layer_db);
}
if (System.IO.File.Exists(output_layer_db)) {
Console.Out.WriteLine("Stored output geographic layer to: " + output_layer_db);
}
}
Conn.Close();
}
} catch (System.Exception error) {
Console.Out.WriteLine(error.Message);
}
}
For more information, see:
Getting Started with CaliperForm
Using the CaliperForm.Connection Class
Using the GISDK Extension Engine In-Process API
See also these specific C# examples for:
©2025 Caliper Corporation | www.caliper.com |