在生成的地图图像上没有绘制形状

时间:2018-02-12 10:03:43

标签: geojson sharpmap

我正在尝试从附加的JSON创建地图图像,图像显示正确的位置但不绘制多边形。

只有在一个特征具有圆形形状的情况下才会发生这种情况,而对于其他形状,它们会被渲染得很好。

以下函数用于从json创建图像。

    public static Image CreateMapImage(string json, 
                KnownTileSource baseMapTile = KnownTileSource.BingRoads)
            {
                double maxX = Double.MinValue,
                    maxY = Double.MinValue,
                    minX = Double.MaxValue,
                    minY = Double.MaxValue;

                var map = new SharpMap.Map(new Size(800, 400));
                map.Layers.Add(new TileLayer(
                    KnownTileSources.Create(
                        baseMapTile, bingKey), "BingRoad"));

                JObject rss = JObject.Parse(json);
                foreach (JObject shape in rss["features"])
                {
                    var jsonReader = new NetTopologySuite.IO.GeoJsonReader();
                    var geom = jsonReader.Read<IGeometry>(shape.ToString(Formatting.None));

                    var fp = new GeometryFeatureProvider(geom);
                    var layer = new VectorLayer("geojson", fp);

                    layer.CoordinateTransformation = new
                        CoordinateTransformationFactory().CreateFromCoordinateSystems(
                            GeographicCoordinateSystem.WGS84,
                            ProjectedCoordinateSystem.WebMercator);

                    layer.Style = new SharpMap.Styles.VectorStyle()
                    {
                        Fill = new SolidBrush(Color.FromArgb(100, 255, 0, 0)),
                        Outline = new Pen(Color.Red, 1.5f),
                        EnableOutline = true
                    };

                    maxX = layer.Envelope.MaxX > maxX ? layer.Envelope.MaxX : maxX;
                    maxY = layer.Envelope.MaxY > maxY ? layer.Envelope.MaxY : maxY;
                    minX = layer.Envelope.MinX < minX ? layer.Envelope.MinX : minX;
                    minY = layer.Envelope.MinY < minY ? layer.Envelope.MinY : minY;

                    map.Layers.Add(layer);
                }

                map.ZoomToBox(new Envelope(new GeoPoint(m

inX, minY), new GeoPoint(maxX, maxY)));
                map.Zoom *= 1.1;

                return map.GetMap();
            }

我正在使用以下Geojson文件来渲染形状

{
	"features": [{
			"id": "4",
			"geometry": {
				"coordinates": [[[73.879909, 18.521356], [73.879617, 18.526641], [73.878744, 18.531869], [73.877299, 18.53698], 
				[73.875299, 18.541921], [73.872765, 18.546636], [73.869725, 18.551074], [73.866212, 18.555186], [73.862265, 18.558928], 
				[73.857928, 18.562258], [73.853246, 18.56514], [73.848273, 18.567542], [73.843062, 18.569438], [73.837671, 18.570808], 
				[73.832158, 18.571635], [73.826584, 18.571912], [73.82101, 18.571635], [73.815497, 18.570808], [73.810105, 18.569438], 
				[73.804894, 18.567542], [73.799921, 18.56514], [73.79524, 18.562258], [73.790902, 18.558928], [73.786955, 18.555186], 
				[73.783442, 18.551074], [73.780402, 18.546636], [73.777868, 18.541921], [73.775868, 18.53698], [73.774423, 18.531869], 
				[73.77355, 18.526641], [73.773258, 18.521356], [73.77355, 18.516071], [73.774423, 18.510843], [73.775868, 18.50573], 
				[73.777868, 18.500789], [73.780402, 18.496072], [73.783442, 18.491633], [73.786955, 18.487519], [73.790902, 18.483776], 
				[73.79524, 18.480444], [73.799921, 18.477561], [73.804894, 18.475158], [73.810105, 18.47326], [73.815497, 18.47189], 
				[73.82101, 18.471062], [73.826584, 18.470785], [73.832158, 18.471062], [73.837671, 18.47189], [73.843062, 18.47326], 
				[73.848273, 18.475158], [73.853246, 18.477561], [73.857928, 18.480444], [73.862265, 18.483776], [73.866212, 18.487519], 
				[73.869725, 18.491633], [73.872765, 18.496072], [73.875299, 18.500789], [73.877299, 18.50573], [73.878744, 18.510843], 
				[73.879617, 18.516071], [73.879909, 18.521356]]],
				"type": "Polygon",
				"bbox": [73.773258, 18.470785, 73.879909, 18.571912]
			},
			"properties": {
				"status": "add",
				"editable": "true",
				"color": {
					"r": 0,
					"g": 255,
					"b": 255,
					"a": 0.45
				},
				"border": {
					"b": 255,
					"g": 255,
					"r": 0,
					"a": 1
				},
				"OBJECTID": "4"
			},
			"type": "Feature",
			"bbox": [73.773258, 18.470785, 73.879909, 18.571912]
		}
	],
	"type": "FeatureCollection"
}

1 个答案:

答案 0 :(得分:1)

定义“反向”坐标转换帮助了我:

var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
layer.CoordinateTransformation = ctf.CreateFromCoordinateSystems(
    ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84,
    ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);

layer.ReverseCoordinateTransformation = ctf.CreateFromCoordinateSystems(
    ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator,
    ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);