Hi,
I have imported the US Census Data for SQL 2008 from CodePlex into a SQL Server 2008 R2 database. The data for County, City and Zip Code works as expected when retrieved and bound using a SqlShapeReader.
The State data has an issue whereby any Geometry data stored in the form of a GEOMETRYCOLLECTION is not rendered? An example of this issue is seen with the first imported record for Alabama:
GEOMETRYCOLLECTION (LINESTRING (-87.26055908203125 33.50421142578125...
I can query this data in SQL Server Management Studio and the regions are rendered as expected.
Any ideas why the xamMap would be silently failing to render these geo-spatial elements?
See the below image for the output when working with the state data:
Many thanx Ivan, will give that a go.
Regards,
Chris
Hi newc1,
SqlShapeReader supports the following geometry primitives: Point, LineString, Polygon, MultiPoint, MultiLineString and MultiPolygon. Unfortunately GeometryCollection is currently not supported, but you could iterate over GeometryCollection's items and add them as a separate objects. Please take a look at the following code snippet:
public static IEnumerable<Dictionary<string, string>> GetData(string connectionString, string commandText){ var list = new List<Dictionary<string, string>>();
var sqlConnection = new SqlConnection { ConnectionString = connectionString }; var sqlCommand = new SqlCommand { Connection = sqlConnection, CommandType = System.Data.CommandType.Text, CommandText = commandText };
sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader();
var geomIndex = -1; var stateNameIndex = -1; var stateIdIndex = -1;
while (reader.Read()) { if (geomIndex == -1) { geomIndex = reader.GetOrdinal("geom"); stateNameIndex = reader.GetOrdinal("StateName"); stateIdIndex = reader.GetOrdinal("StateId"); }
var geometry = GetGeometry((SqlGeometry)reader.GetValue(geomIndex));
if (geometry.Count == 1) { list.Add(new Dictionary<string, string> { { "StateId", reader.GetValue(stateIdIndex).ToString() }, { "StateName", reader.GetValue(stateNameIndex).ToString() }, { "geom", geometry[0] } }); } else if (geometry.Count > 1) { string stateName = null;
for (int i = 0; i < geometry.Count; i++) { if (i == 0) { stateName = reader.GetValue(stateNameIndex).ToString(); }
list.Add(new Dictionary<string, string> { {"StateId", i == 0 ? reader.GetValue(stateIdIndex).ToString() : ""}, {"StateName", stateName}, {"geom", geometry[i]} }); } } }
sqlConnection.Close(); sqlConnection.Dispose();
return list;}
private static IList<string> GetGeometry(SqlGeometry geometry){ var geometries = new List<string>();
if (geometry.STGeometryType().Value == "GeometryCollection") { var numGeometries = geometry.STNumGeometries().Value;
for (int j = 1; j <= numGeometries; j++) { geometries.Add(geometry.STGeometryN(j).ToString());
} } else { geometries.Add(geometry.ToString()); }
return geometries;}
Which produces the following result:
I would recommend submitting a feature request for the GeometryCollection support here: http://devcenter.infragistics.com/Protected/RequestFeature.aspx.
Ivan Kotev