private SharpMap.Geometries.Geometry ParseOgrGeometry(OGR.Geometry OgrGeometry) { byte[] wkbBuffer = new byte[OgrGeometry.WkbSize()]; int i = OgrGeometry.ExportToWkb(wkbBuffer); return(SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(wkbBuffer)); }
private void ParseGeometry(OGR.Geometry geom, string keyStringValue, double keyNumericValue, string labelValue) { int geomType = geom.GetGeometryType(); if (geomType == ogr.wkbUnknown) { return; } // points else if (geomType == ogr.wkbPoint || geomType == ogr.wkbPoint25D) { bool dataSubListExists = false; foreach (Icons l in styleSubList.ChildObjects) { if (l.Name == keyStringValue) { dataSubList = l; dataSubListExists = true; } } if (!dataSubListExists) { dataSubList = new Icons(keyStringValue); //subSubList.DisableExpansion = true; styleSubList.Add(dataSubList); } double[] p = { geom.GetX(0), geom.GetY(0) }; if (needsTransformation) { coordTransform.TransformPoint(p); } WorldWind.Renderable.Icon ic = new WorldWind.Renderable.Icon(labelValue, p[1], p[0], 0); //ic.TextureFileName = Path.Combine(this.PluginDirectory, "icon.png"); ic.TextureFileName = ""; //ic.Width = 12; //ic.Height = 12; dataSubList.Add(ic); //Console.WriteLine(" Found point, contains " + geom.GetPointCount()); waitMessage.Text += "\nFound " + geom.GetGeometryName(); } // linestring or multi-line else if (geomType == ogr.wkbLineString || geomType == ogr.wkbLineString25D) { //Console.WriteLine(" Found " + geom.GetGeometryName() + ", contains " + geom.GetPointCount() + "points, " + geom.GetGeometryCount() + " sub-geometries"); waitMessage.Text += "\nFound " + geom.GetGeometryName() + ", contains " + geom.GetPointCount() + " points, " + geom.GetGeometryCount() + " sub-geometries"; bool dataSubListExists = false; foreach (Icons l in styleSubList.ChildObjects) { if (l.Name == keyStringValue) { dataSubList = l; dataSubListExists = true; } } if (!dataSubListExists) { dataSubList = new Icons(keyStringValue); //subSubList.DisableExpansion = true; styleSubList.Add(dataSubList); } int pointCount = geom.GetPointCount(); Point3d[] p = new Point3d[pointCount]; //TODO: handle 2.5/3D? for (int i = 0; i < pointCount; i++) { double[] point = { geom.GetX(i), geom.GetY(i) }; if (needsTransformation) { coordTransform.TransformPoint(point); } p[i] = new Point3d(point[0], point[1], 0); } //Color lineColor = Color.FromArgb(infoSelector.LineAlpha, infoSelector.LineColor); Color lineColor = InterpolateColor(keyNumericValue, true); LineFeature lf = new LineFeature(labelValue, world, p, lineColor); lf.LineWidth = infoSelector.NumericMinStyle.LineWidth; dataSubList.Add(lf); } // polygon... else if (geomType == ogr.wkbPolygon || geomType == ogr.wkbPolygon25D) { bool dataSubListExists = false; foreach (Icons l in styleSubList.ChildObjects) { if (l.Name == keyStringValue) { dataSubList = l; dataSubListExists = true; } } if (!dataSubListExists) { dataSubList = new Icons(keyStringValue); //subSubList.DisableExpansion = true; styleSubList.Add(dataSubList); } //Console.WriteLine(" Found " + geom.GetGeometryName() + ", contains " + geom.GetGeometryCount() + " sub-geometries"); waitMessage.Text += "\nFound " + geom.GetGeometryName() + ", contains " + geom.GetPointCount() + " points, " + geom.GetGeometryCount() + " sub-geometries"; int numInnerRings = geom.GetGeometryCount() - 1; LinearRing[] innerRings; if (numInnerRings != 0) { innerRings = new LinearRing[numInnerRings]; } else { innerRings = null; } LinearRing outerRing = new LinearRing(); OGR.Geometry ring; //outer ring... ring = geom.GetGeometryRef(0); waitMessage.Text += "\nFound " + ring.GetGeometryName() + ", contains " + ring.GetPointCount() + " points, " + ring.GetGeometryCount() + " sub-geometries"; int pointCount = ring.GetPointCount(); Point3d[] p = new Point3d[pointCount]; for (int k = 0; k < pointCount; k++) { double x = ring.GetX(k); double y = ring.GetY(k); double[] point = { x, y }; if (needsTransformation) { coordTransform.TransformPoint(point); } p[k] = new Point3d(point[0], point[1], 0); } outerRing.Points = p; //inner rings... if (innerRings != null) { for (int i = 1; i < geom.GetGeometryCount(); i++) { ring = geom.GetGeometryRef(i); waitMessage.Text += "\nFound " + ring.GetGeometryName() + ", contains " + ring.GetPointCount() + " points, " + ring.GetGeometryCount() + " sub-geometries"; for (int j = 0; j < ring.GetPointCount(); j++) { int innerRingPointCount = ring.GetPointCount(); Point3d[] q = new Point3d[innerRingPointCount]; for (int k = 0; k < innerRingPointCount; k++) { double x = ring.GetX(k); double y = ring.GetY(k); double[] point = { x, y }; if (needsTransformation) { coordTransform.TransformPoint(point); } q[k] = new Point3d(point[0], point[1], 0); } LinearRing r = new LinearRing(); r.Points = q; innerRings[i - 1] = r; } } } Color fillColor = InterpolateColor(keyNumericValue, false); Color lineColor = InterpolateColor(keyNumericValue, true); PolygonFeature pf = new PolygonFeature( labelValue, world, outerRing, innerRings, fillColor); pf.Outline = infoSelector.NumericMinStyle.OutlinePolygons; pf.OutlineColor = lineColor; dataSubList.Add(pf); } else if (geomType == ogr.wkbMultiPoint || geomType == ogr.wkbMultiPoint25D || geomType == ogr.wkbMultiLineString || geomType == ogr.wkbMultiLineString25D || geomType == ogr.wkbMultiPolygon || geomType == ogr.wkbMultiPolygon25D) { waitMessage.Text += "\nFound " + geom.GetGeometryName() + ", contains " + geom.GetGeometryCount() + " sub-geometries"; ParseGeometry(geom, keyStringValue, keyNumericValue, labelValue); } }
/// <summary> /// Loads the shp and adds it to the LM /// </summary> private void LoadVectors() { Feature feat; try { if (keyDataType == DataType.Numeric) { ogrLayer.ResetReading(); bool setRange = false; while ((feat = ogrLayer.GetNextFeature()) != null) { double data = feat.GetFieldAsDouble(keyFieldIndex); if (!setRange && data != noDataValue) { minDataValue = data; maxDataValue = data; setRange = true; } else if (data != noDataValue) { if (minDataValue > data) { minDataValue = data; } if (maxDataValue < data) { maxDataValue = data; } } } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\nCould not get data value range."); } try { int i = 0; int numFeatures = ogrLayer.GetFeatureCount(1); ogrLayer.ResetReading(); while ((feat = ogrLayer.GetNextFeature()) != null) { if (keyDataType == DataType.Text) { string keyValue = feat.GetFieldAsString(keyFieldIndex); bool loadFeature = true; if (filterString != null) { switch (textFilterType) { case TextFilterType.Exact: if (keyValue == filterString) { loadFeature = true; } else { loadFeature = false; } break; case TextFilterType.Contains: if (keyValue.Contains(filterString)) { loadFeature = true; } else { loadFeature = false; } break; case TextFilterType.Regex: Match m = Regex.Match(keyValue, filterString); if (m.Success) { loadFeature = true; } else { loadFeature = false; } break; } } if (loadFeature) { string labelValue = feat.GetFieldAsString(labelFieldIndex); OGR.Geometry geom = feat.GetGeometryRef(); //Console.WriteLine("Parsing {0} of {1} features.", i+1, numFeatures); waitMessage.Text = string.Format("Parsing {0} of {1} features.", i + 1, numFeatures); ParseGeometry(geom, keyValue, 0, labelValue); feat.Dispose(); i++; } } if (keyDataType == DataType.Numeric) { double keyDataValue = feat.GetFieldAsDouble(keyFieldIndex); bool loadFeature = true; // numeric filtering would happen here... if (loadFeature) { string labelValue = feat.GetFieldAsString(labelFieldIndex); OGR.Geometry geom = feat.GetGeometryRef(); //Console.WriteLine("Parsing {0} of {1} features.", i+1, numFeatures); waitMessage.Text = string.Format("Parsing {0} of {1} features.", i + 1, numFeatures); ParseGeometry(geom, keyDataValue.ToString(), keyDataValue, labelValue); feat.Dispose(); i++; } } } waitMessage.Text = "Loaded " + i + " features of " + numFeatures + "."; ds.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\nLoading aborted."); } }