public static void Run() { //ExStart: LimitPrecisionWhenWritingGeometries var options = new GeoJsonOptions { // write only 3 fractional digits of X and Y coordinates. XYPrecisionModel = PrecisionModel.Rounding(3), // write all fractional digits of Z coordinate (the default, you don't have to specify it) ZPrecisionModel = PrecisionModel.Exact }; var path = RunExamples.GetDataDir() + "LimitPrecisionWhenWritingGeometries_out.json"; using (VectorLayer layer = VectorLayer.Create(path, Drivers.GeoJson, options)) { var point = new Point(); point.X = 1.8888888; point.Y = 1.00123; point.Z = 1.123456789; Feature feature = layer.ConstructFeature(); feature.Geometry = point; layer.Add(feature); } using (VectorLayer layer = VectorLayer.Open(path, Drivers.GeoJson)) { var point = (IPoint)layer[0].Geometry; // 1.889, 1.001, 1.123456789 Console.WriteLine("{0}, {1}, {2}", point.X, point.Y, point.Z); } //ExEnd: LimitPrecisionWhenWritingGeometries }
public static void IterateFilteredFeatures() { var citiesPath = DataDir + "points.geojson"; //ExStart: IterateFilteredFeatures using (var layer = VectorLayer.Open(citiesPath, Drivers.GeoJson)) { // Use attribute index to speed up search by 'population' attribute. // Aspose.GIS builds a new index if it doesn't exist, otherwise existing index is reused. // Any path can be used. var attributeIndexPath = Path.ChangeExtension(citiesPath, "population_out.ix"); layer.UseAttributesIndex(attributeIndexPath, "population"); // Select features based on values of the attribute. Since we use attribute index it is not necessary to // test all features of the layer and filtering time is reduced significantly. var towns = layer.WhereGreaterOrEqual("population", 2000).WhereSmaller("population", 5000); // Print results. Console.WriteLine("Cities where population >= 2000 and population < 5000:"); foreach (var town in towns) { var name = town.GetValue <string>("name"); var population = town.GetValue <int>("population"); Console.WriteLine(name + ", " + population); } Console.WriteLine(); } //ExEnd: IterateFilteredFeatures }
public static void LinesLabelingCurvedWithOffset() { //ExStart: LinesLabelingCurvedWithOffset using (var map = new Map(1000, 634)) { var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) }; var labeling = new SimpleLabeling(labelAttribute: "name") { HaloSize = 1, Placement = new LineLabelPlacement { Alignment = LineLabelAlignment.Curved, Offset = 5, } }; map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer, labeling); map.Padding = 50; map.Render(dataDir + "lines_labeling_curved_offset_out.svg", Renderers.Svg); } //ExEnd: LinesLabelingCurvedWithOffset }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: GetValueOfAFeatureAttribute using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { for (int i = 0; i < layer.Count; i++) { Feature feature = layer[i]; Console.WriteLine("Entry {0} information\n ========================", i); // case 1 string nameValue = feature.GetValue <string>("name"); // attribute name is case-sensitive int ageValue = feature.GetValue <int>("age"); string dobValue = feature.GetValue <DateTime>("dob").ToString(); Console.WriteLine("Attribute value for feature #{0} is: {1}, {2}", nameValue, ageValue, dobValue); // case 2 var objName = feature.GetValue("name"); // attribute name is case-sensitive var objAge = feature.GetValue("age"); var objDob = feature.GetValue("dob"); Console.WriteLine("Attribute object for feature #{0} is: {1}, {2}", objName, objAge, objDob); } } //ExEnd: GetValueOfAFeatureAttribute }
public static void Run() { //ExStart: LimitPrecisionWhenReadingGeometries string path = RunExamples.GetDataDir() + "LimitPrecisionWhenReadingGeometries_out.shp"; using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile)) { var feature = layer.ConstructFeature(); feature.Geometry = new Point(1.10234, 2.09743); layer.Add(feature); } var options = new ShapefileOptions(); // read data as-is. options.XYPrecisionModel = PrecisionModel.Exact; using (VectorLayer layer = VectorLayer.Open(path, Drivers.Shapefile, options)) { var point = (IPoint)layer[0].Geometry; // 1.10234, 2.09743 Console.WriteLine("{0}, {1}", point.X, point.Y); } // truncate all X and Y, so only two fractional digits are left. options.XYPrecisionModel = PrecisionModel.Rounding(2); using (VectorLayer layer = VectorLayer.Open(path, Drivers.Shapefile, options)) { var point = (IPoint)layer[0].Geometry; // 1.1, 2.1 Console.WriteLine("{0}, {1}", point.X, point.Y); } //ExEnd: LimitPrecisionWhenReadingGeometries }
public static void AddMapLayersAndStyles() { //ExStart: AddMapLayersAndStyles using (var map = new Map(800, 476)) { var baseMapSymbolizer = new SimpleFill { FillColor = Color.Salmon, StrokeWidth = 0.75 }; map.Add(VectorLayer.Open(dataDir + "basemap.shp", Drivers.Shapefile), baseMapSymbolizer); var citiesSymbolizer = new SimpleMarker() { FillColor = Color.LightBlue }; citiesSymbolizer.FeatureBasedConfiguration = (feature, symbolizer) => { var population = feature.GetValue <int>("population"); symbolizer.Size = 10 * population / 1000; if (population < 2500) { symbolizer.FillColor = Color.GreenYellow; } }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), citiesSymbolizer); map.Render(dataDir + "cities_out.svg", Renderers.Svg); } //ExEnd: AddMapLayersAndStyles }
public static void GeometryGenerator() { //ExStart: GeometryGenerator using (var map = new Map(500, 200)) { var symbol = new GeometryGenerator { Expression = f => { return(f.Geometry.GetBuffer(30_000)); }, Symbolizer = new SimpleFill { FillColor = Color.LightBlue, StrokeColor = Color.Blue, }, }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol); map.Padding = 20; map.SpatialReferenceSystem = SpatialReferenceSystem.WebMercator; map.Render(dataDir + "geometry_generator_out.svg", Renderers.Svg); } //ExEnd: GeometryGenerator }
public static void PointsLabelingPlaced() { //ExStart: PointsLabelingPlaced using (var map = new Map(500, 200)) { var symbol = new SimpleMarker { FillColor = Color.LightGray, StrokeStyle = StrokeStyle.None }; var labeling = new SimpleLabeling(labelAttribute: "name") { HaloSize = 1, Placement = new PointLabelPlacement { VerticalAnchorPoint = VerticalAnchor.Bottom, HorizontalAnchorPoint = HorizontalAnchor.Left, HorizontalOffset = 2, VerticalOffset = 2, Rotation = 10, } }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol, labeling); map.Padding = 50; map.Render(dataDir + "points_labeling_placed_out.svg", Renderers.Svg); } //ExEnd: PointsLabelingPlaced }
public static void PointsLabelingStyled() { //ExStart: PointsLabelingStyled using (var map = new Map(500, 200)) { var symbol = new SimpleMarker { FillColor = Color.LightGray, StrokeStyle = StrokeStyle.None }; var labeling = new SimpleLabeling(labelAttribute: "name") { HaloSize = 2, HaloColor = Color.LightGray, FontSize = 15, FontStyle = FontStyle.Italic, }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol, labeling); map.Padding = 50; map.Render(dataDir + "points_labeling_styled_out.svg", Renderers.Svg); } //ExEnd: PointsLabelingStyled }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: GetValuesOfAFeatureAttribute using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { foreach (var feature in layer) { // reads all the attributes into an array. object[] all = new object[3]; feature.GetValues(all); Console.WriteLine("all : {0}, {1}, {2}", all); // reads several the attributes into an array. object[] several = new object[2]; feature.GetValues(several); Console.WriteLine("several: {0}, {1}", several); // reads the attributes as dump of objects. var dump = feature.GetValuesDump(); Console.WriteLine("dump : {0}, {1}, {2}", dump); Console.WriteLine(); } } //ExEnd: GetValuesOfAFeatureAttribute }
public static void ChangeMarkerStyleFeatureBased() { //ExStart: ChangeMarkerStyleFeatureBased using (var map = new Map(500, 200)) { var symbol = new SimpleMarker() { FillColor = Color.LightBlue }; symbol.FeatureBasedConfiguration = (feature, symbolizer) => { // retirieve city population (in thousands) from a feature attribute var population = feature.GetValue <int>("population"); // let's increase circle radius by 5 pixels for each million people symbolizer.Size = 5 * population / 1000; // and let's draw cities with less than 2.5M people in green if (population < 2500) { symbolizer.FillColor = Color.GreenYellow; } }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol); map.Padding = 20; map.Render(dataDir + "points_out.svg", Renderers.Svg); } //ExEnd: ChangeMarkerStyleFeatureBased }
public static void FindFeaturesWithinExtent() { var path = DataDir + "points.geojson"; //ExStart: FindFeaturesWithinExtent using (var layer = VectorLayer.Open(path, Drivers.GeoJson)) { // Use spatial index to speed up 'WhereIntersects'. var spatialIndexPath = Path.ChangeExtension(path, ".spatial_out.ix"); layer.UseSpatialIndex(spatialIndexPath); var polygon = Geometry.FromText("Polygon((12.30 50.33, 22.49 54.87, 21.92 42.53, 12.30 50.33))"); var intersecting = layer.WhereIntersects(polygon); Console.WriteLine("Cities within " + polygon.AsText() + ":"); foreach (var feature in intersecting) { var name = feature.GetValue <string>("name"); var location = (IPoint)feature.Geometry; Console.WriteLine($"{name} at ({location.X}, {location.Y})"); } Console.WriteLine(); } //ExEnd: FindFeaturesWithinExtent }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: ExtractFeaturesFromShapeFileToGeoJSON using (VectorLayer inputLayer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { using (VectorLayer outputLayer = VectorLayer.Create(dataDir + "ExtractFeaturesFromShapeFileToGeoJSON_out.json", Drivers.GeoJson)) { outputLayer.CopyAttributes(inputLayer); foreach (Feature inputFeature in inputLayer) { DateTime?date = inputFeature.GetValue <DateTime?>("dob"); if (date == null || date < new DateTime(1982, 1, 1)) { continue; } //Construct a new feature Feature outputFeature = outputLayer.ConstructFeature(); outputFeature.Geometry = inputFeature.Geometry; outputFeature.CopyValues(inputFeature); outputLayer.Add(outputFeature); } } } //ExEnd: ExtractFeaturesFromShapeFileToGeoJSON }
private static void ModifyFeaturesInSingleLayer() { //ExStart: ModifyFeaturesInSingleLayer string sourcePath = Path.Combine(dataDir, "InputShapeFile.shp"); string resultPath = Path.Combine(dataDir, "modified_out.shp"); using (var source = VectorLayer.Open(sourcePath, Drivers.Shapefile)) using (var result = VectorLayer.Create(resultPath, Drivers.Shapefile, source.SpatialReferenceSystem)) { result.CopyAttributes(source); foreach (var feature in source) { // modify the geometry var modifiedGeometry = feature.Geometry.GetBuffer(2.0); feature.Geometry = modifiedGeometry; // modify a feature attribute var attributeValue = feature.GetValue <string>("name"); var modifiedAttributeValue = attributeValue.ToUpper(); feature.SetValue("name", modifiedAttributeValue); result.Add(feature); } } //ExEnd: ModifyFeaturesInSingleLayer }
public static void RenderWithDefaultSettings() { //ExStart: RenderWithDefaultSettings using (var map = new Map(800, 400)) { map.Add(VectorLayer.Open(dataDir + "land.shp", Drivers.Shapefile)); map.Render(dataDir + "land_out.svg", Renderers.Svg); } //ExEnd: RenderWithDefaultSettings }
public static void RenderToSpecificProjection() { //ExStart: RenderToSpecificProjection using (var map = new Map(800, 400)) { map.Add(VectorLayer.Open(dataDir + "land.shp", Drivers.Shapefile)); map.SpatialReferenceSystem = SpatialReferenceSystem.CreateFromEpsg(54024); // World Bonne map.Render(dataDir + "land_out.svg", Renderers.Svg); } //ExEnd: RenderToSpecificProjection }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: GetFeatureCountInLayer using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { Console.WriteLine("Total Features in this file: " + layer.Count); } //ExEnd: GetFeatureCountInLayer }
public static void DefaultFillStyle() { //ExStart: DefaultFillStyle using (var map = new Map(500, 450)) { var symbolizer = new SimpleFill(); map.Add(VectorLayer.Open(dataDir + "polygons.geojson", Drivers.GeoJson), symbolizer); map.Render(dataDir + "polygons_out.svg", Renderers.Svg); } //ExEnd: DefaultFillStyle }
public static void DefaultMarkerStyle() { //ExStart: DefaultMarkerStyle using (var map = new Map(500, 200)) { var symbol = new SimpleMarker(); map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol); map.Padding = 20; map.Render(dataDir + "points_out.svg", Renderers.Svg); } //ExEnd: DefaultMarkerStyle }
public static void ChangeLineStyle() { //ExStart: ChangeLineStyle using (var map = new Map(500, 317)) { var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) }; map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer); map.Render(dataDir + "lines_out.svg", Renderers.Svg); } //ExEnd: ChangeLineStyle }
public static void ChangeFillStyle() { //ExStart: ChangeFillStyle using (var map = new Map(500, 450)) { var symbolizer = new SimpleFill { FillColor = Color.Azure, StrokeColor = Color.Brown }; map.Add(VectorLayer.Open(dataDir + "polygons.geojson", Drivers.GeoJson), symbolizer); map.Render(dataDir + "polygons_out.svg", Renderers.Svg); } //ExEnd: ChangeFillStyle }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: IterateOverFeaturesInLayer using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { foreach (Feature feature in layer) { Console.WriteLine(feature.Geometry.AsText()); } } //ExEnd: IterateOverFeaturesInLayer }
public static void Run() { string dataDir = RunExamples.GetDataDir(); //ExStart: FilterFeaturesByAttributeValue using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) { // all features with the date value in the attribute "dob" later than 1982-01-01. foreach (Feature feature in layer.WhereGreater("dob", new DateTime(1982, 1, 1, 0, 0, 0))) { Console.WriteLine(feature.GetValue <DateTime>("dob").ToShortDateString()); } } //ExEnd: FilterFeaturesByAttributeValue }
public static void OpenFileGdbAsLayer() { //ExStart:OpenFileGdbAsLayer //FileGDB can still be opened as a single layer. In this case, the opened layer will merge all layers inside FileGDB. using (var layer = VectorLayer.Open(dataDir + "ThreeLayers.gdb", Drivers.FileGdb)) { Console.WriteLine("All layers in FileGDB has {0} features", layer.Count); foreach (var feature in layer) { Console.WriteLine(feature.Geometry.AsText()); } } //ExEnd: OpenFileGdbAsLayer }
public static void LinesLabelingFeatureBased() { //ExStart: LinesLabelingFeatureBased using (var map = new Map(1000, 634)) { var lineSymbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xae, 0xd9, 0xfd) }; lineSymbolizer.FeatureBasedConfiguration = (feature, symbolizer) => { if (feature.GetValue <string>("NAM") == "UNK") { symbolizer.Width = 1; symbolizer.Style = StrokeStyle.Dash; } }; var lineLabeling = new SimpleLabeling(labelAttribute: "name") { HaloSize = 1, Placement = new LineLabelPlacement { Alignment = LineLabelAlignment.Parallel, }, FontSize = 20, FeatureBasedConfiguration = (feature, labeling) => { if (feature.GetValue <string>("NAM") == "UNK") { // change labeling properties for some features. labeling.FontStyle = FontStyle.Italic; labeling.FontSize = 10; labeling.Priority = -1; var placement = (LineLabelPlacement)labeling.Placement; placement.Alignment = LineLabelAlignment.Curved; placement.Offset = 5; } } }; map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), lineSymbolizer, lineLabeling); map.Padding = 50; map.Render(dataDir + "lines_labeling_feature_based_out.svg", Renderers.Svg); } //ExEnd: LinesLabelingFeatureBased }
public static void Run() { //ExStart: ConvertGeoJsonLayerToLayerInFileGdbDataset var geoJsonPath = RunExamples.GetDataDir() + "ConvertGeoJsonLayerToLayerInFileGdbDataset_out.json"; using (VectorLayer layer = VectorLayer.Create(geoJsonPath, Drivers.GeoJson)) { layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String)); layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer)); Feature firstFeature = layer.ConstructFeature(); firstFeature.Geometry = new Point(33.97, -118.25); firstFeature.SetValue("name", "John"); firstFeature.SetValue("age", 23); layer.Add(firstFeature); Feature secondFeature = layer.ConstructFeature(); secondFeature.Geometry = new Point(35.81, -96.28); secondFeature.SetValue("name", "Mary"); secondFeature.SetValue("age", 54); layer.Add(secondFeature); } // -- // -- copy test dataset, to avoid modification of test data. var sourceFile = RunExamples.GetDataDir() + "ThreeLayers.gdb"; var destinationFile = RunExamples.GetDataDir() + "ThreeLayersCopy_out.gdb"; RunExamples.CopyDirectory(sourceFile, destinationFile); // -- using (var geoJsonLayer = VectorLayer.Open(geoJsonPath, Drivers.GeoJson)) { using (var fileGdbDataset = Dataset.Open(destinationFile, Drivers.FileGdb)) using (var fileGdbLayer = fileGdbDataset.CreateLayer("new_layer", SpatialReferenceSystem.Wgs84)) { fileGdbLayer.CopyAttributes(geoJsonLayer); foreach (var feature in geoJsonLayer) { fileGdbLayer.Add(feature); } } } //ExEnd: ConvertGeoJsonLayerToLayerInFileGdbDataset }
public static void ChangeMarkerStyle() { //ExStart: ChangeMarkerStyle using (var map = new Map(500, 200)) { var symbol = new SimpleMarker() { Size = 7, StrokeWidth = 1, FillColor = Color.Red }; map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol); map.Padding = 20; map.Render(dataDir + "points_out.svg", Renderers.Svg); } //ExEnd: ChangeMarkerStyle }
public static void Run() { //ExStart: ReadGeoJsonFromStream const string geoJson = @"{""type"":""FeatureCollection"",""features"":[ {""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[0, 1]},""properties"":{""name"":""John""}}, {""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[2, 3]},""properties"":{""name"":""Mary""}} ]}"; using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(geoJson))) using (var layer = VectorLayer.Open(AbstractPath.FromStream(memoryStream), Drivers.GeoJson)) { Console.WriteLine(layer.Count); // 2 Console.WriteLine(layer[1].GetValue <string>("name")); // Mary } //ExEnd: ReadGeoJsonFromStream }
public static void Run() { //ExStart: AccessFeaturesInTopoJson string sampleTopoJsonPath = RunExamples.GetDataDir() + "sample.topojson"; StringBuilder builder = new StringBuilder(); using (VectorLayer layer = VectorLayer.Open(sampleTopoJsonPath, Drivers.TopoJson)) { foreach (Feature feature in layer) { // get id property int id = feature.GetValue <int>("id"); // get name of the object that contains this feature string objectName = feature.GetValue <string>("topojson_object_name"); // get name attribute property, located inside 'properties' object string name = feature.GetValue <string>("name"); // get geometry of the feature. string geometry = feature.Geometry.AsText(); builder.AppendFormat("Feature with ID {0}:\n", id); builder.AppendFormat("Object Name = {0}\n", objectName); builder.AppendFormat("Name = {0}\n", name); builder.AppendFormat("Geometry = {0}\n", geometry); } } Console.WriteLine("Output:"); Console.WriteLine(builder.ToString()); // Output: // Feature with ID 0: // Object Name = named_object_1 // Name = point_feature // Geometry = POINT (102 0.5) // Feature with ID 1: // Object Name = named_object_1 // Name = line_feature // Geometry = LINESTRING (102 0, 103 1, 104 0, 105 1) // Feature with ID 2: // Object Name = named_object_2 // Name = polygon_feature // Geometry = POLYGON ((100 0, 100 1, 101 1, 101 0, 100 0)) //ExEnd: AccessFeaturesInTopoJson }
public static void Run() { //ExStart: ImportSLD using (var map = new Map(500, 320)) { // open a layer containing the data var layer = VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson); // create a map layer (a styled representation of the data) var mapLayer = new VectorMapLayer(layer); // import a style from an SLD document mapLayer.ImportSld(dataDir + "lines.sld"); // add the styled layer to the map and render it map.Add(mapLayer); map.Render(dataDir + "lines_sld_style_out.png", Renderers.Png); } //ExEnd: ImportSLD }