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 ValidateOnWriteObeyingSpecifications() { //ExStart: ValidateOnWriteObeyingSpecifications LineString lineStrinWithOnePoint = new LineString(); lineStrinWithOnePoint.AddPoint(0, 0); GeoJsonOptions options = new GeoJsonOptions(); options.ValidateGeometriesOnWrite = false; using (var layer = Drivers.GeoJson.CreateLayer(dataDir + "ValidateOnWriteObeyingSpecifications_out.json", options)) { Feature feature = layer.ConstructFeature(); // GeoJSON specification says that line string must have at least two coordinates. feature.Geometry = lineStrinWithOnePoint; try { // Geometry of feature doesn't match data format specification, so exception is thrown // regardless what ValidateGeometriesOnWrite option is. layer.Add(feature); } catch (GisException e) { Console.WriteLine(e.Message); } } //ExEnd: ValidateOnWriteObeyingSpecifications }
public static void ValidateOnWrite() { //ExStart: ValidateGeometriesOnWrite var exteriorRing = new LinearRing(); exteriorRing.AddPoint(0, 0); exteriorRing.AddPoint(0, 1); exteriorRing.AddPoint(1, 1); exteriorRing.AddPoint(1, 0); exteriorRing.AddPoint(0, 0); var interiorRing = new LinearRing(); interiorRing.AddPoint(0.5, 0.5); interiorRing.AddPoint(1, 0.5); interiorRing.AddPoint(1, 1); interiorRing.AddPoint(0.5, 1); interiorRing.AddPoint(0.5, 0.5); var invalidPolygon = new Polygon(); invalidPolygon.ExteriorRing = exteriorRing; invalidPolygon.AddInteriorRing(interiorRing); // invalidPolygon.IsValid == false, since polygon rings share segments (have infinite number of intersection points) GeoJsonOptions options = new GeoJsonOptions(); options.ValidateGeometriesOnWrite = false; // false is default File.Delete(dataDir + "not_validated_data_out.shp"); using (var nonValidatingLayer = Drivers.GeoJson.CreateLayer(dataDir + "not_validated_data_out.shp", options)) { Feature feature = nonValidatingLayer.ConstructFeature(); feature.Geometry = invalidPolygon; // no exception is thrown, since ValidateGeometriesOnWrite == false, and GeoJson specification doesn't say that rings of polygon can't share segments. nonValidatingLayer.Add(feature); } options.ValidateGeometriesOnWrite = true; File.Delete(dataDir + "validated_data_out.shp"); using (var validatingLayer = Drivers.GeoJson.CreateLayer(dataDir + "validated_data_out.shp", options)) { Feature feature = validatingLayer.ConstructFeature(); feature.Geometry = invalidPolygon; try { validatingLayer.Add(feature); // GisException is thrown, since polygon is not valid } catch (GisException e) { Console.WriteLine(e.Message); } } //ExEnd: ValidateGeometriesOnWrite }
public static GeoJsonOptions ToOptions(this NxGeoJsonOptions nxoptions) { if (nxoptions == null) { return(null); } var options = new GeoJsonOptions(); if (nxoptions.Buffer.HasValue) { options.WithBuffer(nxoptions.Buffer.Value); } if (nxoptions.Cluster.HasValue) { options.WithCluster(nxoptions.Cluster.Value); } if (nxoptions.ClusterMaxZoom.HasValue) { options.WithClusterMaxZoom(nxoptions.ClusterMaxZoom.Value); } if (nxoptions.ClusterRadius.HasValue) { options.WithClusterRadius(nxoptions.ClusterRadius.Value); } if (nxoptions.LineMetrics.HasValue) { options.WithLineMetrics(nxoptions.LineMetrics.Value); } if (nxoptions.MaxZoom.HasValue) { options.WithMaxZoom(nxoptions.MaxZoom.Value); } if (nxoptions.MinZoom.HasValue) { options.WithMinZoom(nxoptions.MinZoom.Value); } if (nxoptions.Tolerance.HasValue) { options.WithTolerance(nxoptions.Tolerance.Value); } return(options); }
public static void Run() { //ExStart: SpecifyLinearizationTolerance // If file format does not support curve geometries, we linearize them on write. // This example shows how to specify tolerance of the linearization. var options = new GeoJsonOptions { // linearized geometry must be within 1e-4 from curve geometry LinearizationTolerance = 1e-4, }; string path = RunExamples.GetDataDir() + "SpecifyLinearizationTolerance_out.json"; using (VectorLayer layer = VectorLayer.Create(path, Drivers.GeoJson, options)) { var curveGeometry = Geometry.FromText("CircularString (0 0, 1 1, 2 0)"); var feature = layer.ConstructFeature(); feature.Geometry = curveGeometry; // geometry is linearized with tolerance 1e-4 layer.Add(feature); } //ExEnd: SpecifyLinearizationTolerance }