private void CreateRedlineLayer() { MgMapBase map = mgMapViewer1.GetMap(); MgdServiceFactory fact = new MgdServiceFactory(); MgdFeatureService featSvc = (MgdFeatureService)fact.CreateService(MgServiceType.FeatureService); MgResourceService resSvc = (MgResourceService)fact.CreateService(MgServiceType.ResourceService); //Note that mg-desktop does not have a concept of sessions like the //official MapGuide API, but it *does* allow session-based resources //as a way of having temporary resources. Such resources will reside //in a special directory for session resources (specified in Platform.ini) // //You can plug whatever string as the session id, but the resource identifier //must satisfy the session id pattern: // // Session:<session id string>//Path/To/Your.ResourceType // //These files are removed with MgPlatform.Terminate(), which is called in this //application as part of the exiting process. string sessionId = Guid.NewGuid().ToString(); MgResourceIdentifier fsId = new MgResourceIdentifier("Session:" + sessionId + "//Redline.FeatureSource"); MgResourceIdentifier ldfId = new MgResourceIdentifier("Session:" + sessionId + "//Redline.LayerDefinition"); //Create our point redline schema. It looks like this: // // Default // Redline // ID (int32, autogenerated) // Geometry (coordinate system same as map string featureClass = "Default:Redline"; string geometry = "Geometry"; MgFeatureSchema schema = new MgFeatureSchema("Default", "Redline schema"); MgClassDefinition cls = new MgClassDefinition(); cls.Name = "Redline"; MgDataPropertyDefinition id = new MgDataPropertyDefinition("ID"); id.DataType = MgPropertyType.Int32; id.SetAutoGeneration(true); MgGeometricPropertyDefinition geom = new MgGeometricPropertyDefinition(geometry); geom.SpatialContextAssociation = "Default"; geom.GeometryTypes = MgFeatureGeometricType.Curve | MgFeatureGeometricType.Point | MgFeatureGeometricType.Solid | MgFeatureGeometricType.Surface; MgPropertyDefinitionCollection clsProps = cls.GetProperties(); clsProps.Add(id); clsProps.Add(geom); MgPropertyDefinitionCollection idProps = cls.GetIdentityProperties(); idProps.Add(id); cls.DefaultGeometryPropertyName = geometry; MgClassDefinitionCollection classes = schema.GetClasses(); classes.Add(cls); //Create the feature source with this schema. We use the map's //coordinate system for the feature source to ensure features //that we create, will line up with the map MgFileFeatureSourceParams create = new MgFileFeatureSourceParams("OSGeo.SDF", "Default", map.GetMapSRS(), schema); featSvc.CreateFeatureSource(fsId, create); //Then create the layer definition. RedlineLayer.xml contains the template string xml = string.Format(File.ReadAllText("RedlineLayer.xml"), fsId.ToString(), featureClass, geometry); var bytes = Encoding.UTF8.GetBytes(xml); MgByteSource source = new MgByteSource(bytes, bytes.Length); resSvc.SetResource(ldfId, source.GetReader(), null); //Now create the runtime layer and add to map _pointLayer = new MgdLayer(ldfId, resSvc); _pointLayer.LegendLabel = "Redlining"; _pointLayer.Name = "Redline"; _pointLayer.Visible = true; _pointLayer.Selectable = true; _pointLayer.DisplayInLegend = true; var layers = map.GetLayers(); layers.Insert(0, _pointLayer); }
private void btnCreatePointsOfInterest_Click(object sender, EventArgs e) { string layerName = "Points"; string legendLabel = "Points of Interest"; string groupName = "Analysis"; MgMapViewerProvider provider = _viewer.GetProvider(); MgResourceService resourceService = (MgResourceService)provider.CreateService(MgServiceType.ResourceService); //We use MgdFeatureService instead of MgFeatureService as this sample demonstrates APIs unique to mg-desktop MgdFeatureService featureService = (MgdFeatureService)provider.CreateService(MgServiceType.FeatureService); MgMapBase map = _viewer.GetMap(); MgLayerCollection layers = map.GetLayers(); MgLayerGroupCollection groups = map.GetLayerGroups(); //NOTE: mg-desktop has no formal concept of a session repository, but we still //support session-based resources as a form of temporary resource (for the duration //of the application). Session ids in mg-desktop can be any arbitrary string string sessionId = Guid.NewGuid().ToString(); if (layers.IndexOf(layerName) >= 0) { MessageBox.Show("Layer (" + layerName + ") already created"); return; } //---------------------------------------------------// // Create a feature source with point data. // (The Sheboygan sample data does not contain such data, // so we"ll create it.) // Create a feature class definition for the new feature source MgClassDefinition classDefinition = new MgClassDefinition(); classDefinition.SetName("Points"); classDefinition.SetDescription("Feature class with point data."); classDefinition.SetDefaultGeometryPropertyName("GEOM"); MgPropertyDefinitionCollection idProps = classDefinition.GetIdentityProperties(); MgPropertyDefinitionCollection clsProps = classDefinition.GetProperties(); // Create an identify property MgDataPropertyDefinition identityProperty = new MgDataPropertyDefinition("KEY"); identityProperty.SetDataType(MgPropertyType.Int32); identityProperty.SetAutoGeneration(true); identityProperty.SetReadOnly(true); // Add the identity property to the class definition clsProps.Add(identityProperty); idProps.Add(identityProperty); // Create a name property MgDataPropertyDefinition nameProperty = new MgDataPropertyDefinition("NAME"); nameProperty.SetDataType(MgPropertyType.String); // Add the name property to the class definition clsProps.Add(nameProperty); // Create a geometry property MgGeometricPropertyDefinition geometryProperty = new MgGeometricPropertyDefinition("GEOM"); geometryProperty.SetGeometryTypes(MgFeatureGeometricType.Point); // Add the geometry property to the class definition clsProps.Add(geometryProperty); // Create a feature schema MgFeatureSchema featureSchema = new MgFeatureSchema("PointSchema", "Point schema"); MgClassDefinitionCollection classes = featureSchema.GetClasses(); // Add the feature schema to the class definition classes.Add(classDefinition); // Create the feature source String featureSourceName = "Library://Samples/DevGuide/Data/points.FeatureSource"; MgResourceIdentifier resourceIdentifier = new MgResourceIdentifier(featureSourceName); //wkt = "LOCALCS[\"*XY-MT*\",LOCAL_DATUM[\"*X-Y*\",10000],UNIT[\"Meter\", 1],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]"; String wkt = "GEOGCS[\"LL84\",DATUM[\"WGS84\",SPHEROID[\"WGS84\",6378137.000,298.25722293]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.01745329251994]]"; MgFileFeatureSourceParams sdfParams = new MgFileFeatureSourceParams("OSGeo.SDF", "LatLong", wkt, featureSchema); featureService.CreateFeatureSource(resourceIdentifier, sdfParams); // We need to add some data to the sdf before using it. The spatial context // reader must have an extent. MgBatchPropertyCollection batchPropertyCollection = new MgBatchPropertyCollection(); MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter(); MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter(); MgGeometryFactory geometryFactory = new MgGeometryFactory(); // Make four points batchPropertyCollection.Add(MakePoint("Point A", -87.727, 43.748, wktReaderWriter, agfReaderWriter)); batchPropertyCollection.Add(MakePoint("Point B", -87.728, 43.730, wktReaderWriter, agfReaderWriter)); batchPropertyCollection.Add(MakePoint("Point C", -87.726, 43.750, wktReaderWriter, agfReaderWriter)); batchPropertyCollection.Add(MakePoint("Point D", -87.728, 43.750, wktReaderWriter, agfReaderWriter)); // Old way commented out /* * // Add the batch property collection to the feature source * MgInsertFeatures cmd = new MgInsertFeatures("Points", batchPropertyCollection); * MgFeatureCommandCollection featureCommandCollection = new MgFeatureCommandCollection(); * featureCommandCollection.Add(cmd); * * // Execute the "add" commands * featureService.UpdateFeatures(resourceIdentifier, featureCommandCollection, false); */ // Here's the mg-desktop way MgFeatureReader insertResult = featureService.InsertFeatures(resourceIdentifier, "Points", batchPropertyCollection); insertResult.Close(); // ... //---------------------------------------------------// // Create a new layer LayerDefinitionFactory factory = new LayerDefinitionFactory(); // Create a mark symbol String resourceId = "Library://Samples/Sheboygan/Symbols/BasicSymbols.SymbolLibrary"; String symbolName = "PushPin"; String width = "24"; // unit = points String height = "24"; // unit = points String color = "FFFF0000"; String markSymbol = factory.CreateMarkSymbol(resourceId, symbolName, width, height, color); // Create a text symbol String text = "ID"; String fontHeight = "12"; String foregroundColor = "FF000000"; String textSymbol = factory.CreateTextSymbol(text, fontHeight, foregroundColor); // Create a point rule. String ruleLegendLabel = "trees"; String filter = ""; String pointRule = factory.CreatePointRule(ruleLegendLabel, filter, textSymbol, markSymbol); // Create a point type style. String pointTypeStyle = factory.CreatePointTypeStyle(pointRule); // Create a scale range. String minScale = "0"; String maxScale = "1000000000000"; String pointScaleRange = factory.CreateScaleRange(minScale, maxScale, pointTypeStyle); // Create the layer definiton. String featureName = "PointSchema:Points"; String geometry = "GEOM"; String layerDefinition = factory.CreateLayerDefinition(featureSourceName, featureName, geometry, pointScaleRange); //---------------------------------------------------// // ... XmlDocument domDocument = new XmlDocument(); domDocument.LoadXml(layerDefinition); //Add the layer to the map // TODO: Should probably validate this XML content using (MemoryStream ms = new MemoryStream()) { domDocument.Save(ms); ms.Position = 0L; //Note we do this to ensure our XML content is free of any BOM characters byte[] ldfBytes = ms.ToArray(); Encoding utf8 = Encoding.UTF8; String layerDefStr = new String(utf8.GetChars(ldfBytes)); ldfBytes = new byte[layerDefStr.Length - 1]; int byteCount = utf8.GetBytes(layerDefStr, 1, layerDefStr.Length - 1, ldfBytes, 0); // Save the new layer definition to the session repository MgByteSource byteSource = new MgByteSource(ldfBytes, ldfBytes.Length); MgResourceIdentifier resourceID = new MgResourceIdentifier("Session:" + sessionId + "//" + layerName + ".LayerDefinition"); resourceService.SetResource(resourceID, byteSource.GetReader(), null); //Insert this layer to our map MgdLayer newLayer = new MgdLayer(resourceID, resourceService); newLayer.Name = layerName; newLayer.LegendLabel = legendLabel; newLayer.DisplayInLegend = true; newLayer.SetVisible(true); layers.Insert(0, newLayer); //Create analysis group if not already created if (groups.IndexOf(groupName) < 0) { MgLayerGroup group = new MgLayerGroup(groupName); group.LegendLabel = groupName; group.DisplayInLegend = true; groups.Add(group); newLayer.Group = group; } MessageBox.Show("Layer (" + layerName + ") created"); _viewer.RefreshMap(); IMapLegend legend = Shell.Instance.Legend; if (legend != null) { legend.RefreshLegend(); } } }
private void CheckRedlineLayer() { if (_redlineLayer == null) { MgdMap map = (MgdMap)_viewer.GetMap(); MgMapViewerProvider provider = _viewer.GetProvider(); MgdFeatureService featSvc = (MgdFeatureService)provider.CreateService(MgServiceType.FeatureService); MgResourceService resSvc = (MgResourceService)provider.CreateService(MgServiceType.ResourceService); string sessionId = Guid.NewGuid().ToString(); MgResourceIdentifier fsId = new MgResourceIdentifier("Session:" + sessionId + "//Redline.FeatureSource"); MgResourceIdentifier ldfId = new MgResourceIdentifier("Session:" + sessionId + "//Redline.LayerDefinition"); string featureClass = "Default:Redline"; string geometry = "Geometry"; string xml = string.Format(Layers.Redline, fsId.ToString(), featureClass, geometry); //Construct our schema for the redline data store MgFeatureSchema schema = new MgFeatureSchema("Default", "Redline schema"); MgClassDefinition cls = new MgClassDefinition(); cls.Name = "Redline"; MgDataPropertyDefinition id = new MgDataPropertyDefinition("ID"); id.DataType = MgPropertyType.Int32; id.SetAutoGeneration(true); MgGeometricPropertyDefinition geom = new MgGeometricPropertyDefinition(geometry); geom.SpatialContextAssociation = "Default"; geom.GeometryTypes = MgFeatureGeometricType.Curve | MgFeatureGeometricType.Point | MgFeatureGeometricType.Solid | MgFeatureGeometricType.Surface; MgPropertyDefinitionCollection clsProps = cls.GetProperties(); clsProps.Add(id); clsProps.Add(geom); MgPropertyDefinitionCollection idProps = cls.GetIdentityProperties(); idProps.Add(id); cls.DefaultGeometryPropertyName = geometry; MgClassDefinitionCollection classes = schema.GetClasses(); classes.Add(cls); //Create the feature source MgFileFeatureSourceParams create = new MgFileFeatureSourceParams("OSGeo.SDF", "Default", map.GetMapSRS(), schema); featSvc.CreateFeatureSource(fsId, create); //Then the layer definition byte[] bytes = Encoding.UTF8.GetBytes(xml); MgByteSource source = new MgByteSource(bytes, bytes.Length); resSvc.SetResource(ldfId, source.GetReader(), null); //Now create the runtime layer and add to map _redlineLayer = new MgdLayer(ldfId, resSvc); _redlineLayer.LegendLabel = "Redlining"; _redlineLayer.Name = "Redline"; _redlineLayer.Visible = true; _redlineLayer.Selectable = true; _redlineLayer.DisplayInLegend = true; MgLayerCollection layers = map.GetLayers(); layers.Insert(0, _redlineLayer); } }