Anywhere.ArcGIS
Anywhere.ArcGIS copied to clipboard
Some problems with MultiPolygon and MultiLineString GeoJson
I create this method, that connects to some Esri MapServer and gets all geometries and creates a GEOJSON:
public async Task<FeatureCollection<IGeoJsonGeometry>> QueryAllFromMapserviceLayer(string server, string service, string layer)
{
//http://app.anp.gov.br/arcgis/rest/services/ANP_Public_Confidential/MapServer/0/query
var gateway = new PortalGateway(server); //"http://app.anp.gov.br/arcgis/"
var query = new Query((service + "/MapServer/" + layer).AsEndpoint());
query.ReturnGeometry = true;
FeatureCollection<IGeoJsonGeometry> featureCollectionGeoJson = null;
int? Wkid = 0;
if (await this.GetLayerType(server, service, layer) == "esriGeometryPolyline")
{
var result = await gateway.BatchQuery<Anywhere.ArcGIS.Common.Polyline>(query);
Wkid = result.SpatialReference.Wkid;
//var count = result.Features.Count();
featureCollectionGeoJson = FeatureCollectionExtensions.ToFeatureCollection<Anywhere.ArcGIS.Common.Polyline>(result.Features.ToList());
}
if (await this.GetLayerType(server, service, layer) == "esriGeometryPoint")
{
var result = await gateway.BatchQuery<Anywhere.ArcGIS.Common.Point>(query);
Wkid = result.SpatialReference.Wkid;
//var count = result.Features.Count();
featureCollectionGeoJson = FeatureCollectionExtensions.ToFeatureCollection<Anywhere.ArcGIS.Common.Point>(result.Features.ToList());
}
if (await this.GetLayerType(server, service, layer) == "esriGeometryPolygon")
{
var result = await gateway.BatchQuery<Anywhere.ArcGIS.Common.Polygon>(query);
Wkid = result.SpatialReference.Wkid;
//var count = result.Features.Count();
featureCollectionGeoJson = FeatureCollectionExtensions.ToFeatureCollection<Anywhere.ArcGIS.Common.Polygon>(result.Features.ToList());
//por algum motivo ao gerar o geojson algumas features estão vindo zuadas... essa trecho remove as features zuadas do poligono
foreach(var temp in featureCollectionGeoJson.Features)
{
PointCollectionList fixedCoords = new PointCollectionList();
foreach (var coord in ((GeoJsonPolygon)temp.Geometry).Coordinates)
{
if (coord.Count > 3)
fixedCoords.Add(coord);
}
((GeoJsonPolygon)temp.Geometry).Coordinates = fixedCoords;
}
}
foreach (var geom in featureCollectionGeoJson.Features)
{
geom.Id = Guid.NewGuid().ToString();
}
featureCollectionGeoJson.Type = "FeatureCollection";
featureCollectionGeoJson.CoordinateReferenceSystem = new Crs { Type = "name", Properties = new CrsProperties { Name = Wkid.HasValue ? "EPSG:" + Wkid : null} };
return featureCollectionGeoJson;
}
I used the following parameters:
- server=http://app.anp.gov.br/arcgis/
- service=ANP_Public_Confidential
- layer=2
The problem is that layer has some multilines then the geojson generated is invalid... Not represent the same features that original.
I think the problem is in this line:
featureCollectionGeoJson = FeatureCollectionExtensions.ToFeatureCollection<Anywhere.ArcGIS.Common.Polyline>(result.Features.ToList());
I think the lib need to create a new model for these kinds of geometries.
I've never really used the GeoJSON conversion in anger so there are likely some issues, especially with something like you're trying to convert. Since ArcGIS Server can output GeoJSON natively now I haven't looked at updating it either. If you're stuck on an old version you'll have to look at converting it yourself or using another lib to do it.