Skip to content

Commit

Permalink
allow for geometryType:esri-json search geometry
Browse files Browse the repository at this point in the history
closes #42, refs #8
  • Loading branch information
steveoh committed Jun 7, 2017
1 parent 359912b commit 2839bfe
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 84 deletions.
16 changes: 8 additions & 8 deletions WebAPI.Search.Soe.Tests/Commands/ExtractGeometryCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Point
[Test]
public void FractionalCoordinatesDeserializesCorrectly()
{
var command = new ExtractGeometryCommand("point:[1.1231236, 2.321561]");
var command = new ExtractGeometryCommand("point:[1.1231236, 2.321561]", wkid: 26912);
command.Run();

var result = command.Result;
Expand All @@ -26,7 +26,7 @@ public void FractionalCoordinatesDeserializesCorrectly()
[Test]
public void NegativeCoordinatesDeserializesCorrectly()
{
var command = new ExtractGeometryCommand("point:[1,-2]");
var command = new ExtractGeometryCommand("point:[1,-2]", wkid: 26912);
command.Run();

var result = command.Result;
Expand All @@ -39,7 +39,7 @@ public void NegativeCoordinatesDeserializesCorrectly()
[Test]
public void MissingBracketsDeserializesCorrectly()
{
var command = new ExtractGeometryCommand("point:1,2");
var command = new ExtractGeometryCommand("point:1,2", wkid: 26912);
command.Run();

var result = command.Result;
Expand All @@ -52,7 +52,7 @@ public void MissingBracketsDeserializesCorrectly()
[Test]
public void SimpleCoordinatesDeserializesCorrectly()
{
var command = new ExtractGeometryCommand("point:[1, 2]");
var command = new ExtractGeometryCommand("point:[1, 2]", wkid: 26912);
command.Run();

var result = command.Result;
Expand All @@ -69,14 +69,14 @@ public class Polyline
[Test, ExpectedException(typeof(ArgumentException))]
public void LineThrowsErrorUnlessThereAreTwoOrMorePoints()
{
var command = new ExtractGeometryCommand("polyline:[1]");
var command = new ExtractGeometryCommand("polyline:[1]", wkid: 26912);
command.Run();
}

[Test]
public void SimplePolylineDeserializesCorreclty()
{
var command = new ExtractGeometryCommand("polyline:[[1, 2],[2,3]]");
var command = new ExtractGeometryCommand("polyline:[[1, 2],[2,3]]", wkid: 26912);
command.Run();

var result = command.Result;
Expand All @@ -93,14 +93,14 @@ public class Polygon
[Test, ExpectedException(typeof(ArgumentException))]
public void PolygonThrowsErrorUnlessThereAreThreeOrMorePoints()
{
var command = new ExtractGeometryCommand("polygon:[1, 2]");
var command = new ExtractGeometryCommand("polygon:[1, 2]", wkid: 26912);
command.Run();
}

[Test]
public void SimplePolygonDeserializesCorreclty()
{
var command = new ExtractGeometryCommand("polygon:[[1, 2],[2,3],[1, 2]]");
var command = new ExtractGeometryCommand("polygon:[[1, 2],[2,3],[1, 2]]", wkid: 26912);
command.Run();

var result = command.Result;
Expand Down
194 changes: 135 additions & 59 deletions WebAPI.Search.Soe/Commands/ExtractGeometryCommand.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System;
using System.Collections.ObjectModel;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SOESupport;
using Soe.Common.Infastructure.Commands;
using WebAPI.Search.Soe.Models;

namespace WebAPI.Search.Soe.Commands
{
public class ExtractGeometryCommand : Command<GeometryContainer>
{
private string _geometryText;
private bool _ok = true;
private readonly string _geometryText;
private readonly int _wkid;

public ExtractGeometryCommand(string geometryText)
public ExtractGeometryCommand(string geometryText, int wkid)
{
_geometryText = geometryText;
_wkid = wkid;
}

public override string ToString()
Expand All @@ -25,84 +27,158 @@ protected override void Execute()
{
var container = new GeometryContainer();

var parts = _geometryText.Split(new[] {':'});
var geometryType = _geometryText.Substring(0, _geometryText.IndexOf(':')).ToUpperInvariant();
esriGeometryType esriGeometryType;

switch (geometryType)
{
case "ENVELOPE":
{
esriGeometryType = esriGeometryType.esriGeometryEnvelope;
break;
}
case "POINT":
{
esriGeometryType = esriGeometryType.esriGeometryPoint;
break;
}
case "MULTIPOINT":
{
esriGeometryType = esriGeometryType.esriGeometryMultipoint;
break;
}
case "POLYLINE":
{
esriGeometryType = esriGeometryType.esriGeometryPolyline;
break;
}
case "POLYGON":
{
esriGeometryType = esriGeometryType.esriGeometryPolygon;
break;
}
default:
{
Result = CheckForLegacyGeometryInput(container, _geometryText);
return;
}
}

var geometryText = _geometryText.Remove(0, _geometryText.IndexOf(':') + 1);

container.Type = geometryType;
container.Geometry = Conversion.ToGeometry(geometryText, esriGeometryType);

if (container.Geometry == null)
{
container = CheckForLegacyGeometryInput(container, _geometryText);
}

if (container.Geometry != null && container.Geometry.SpatialReference == null)
{
var srFactory = new SpatialReferenceEnvironmentClass();

var isProjected = true;
ISpatialReference newSpatialRefefence = null;

try
{
newSpatialRefefence = srFactory.CreateProjectedCoordinateSystem(_wkid);
}
catch (ArgumentException)
{
isProjected = false;
}

if (!isProjected)
{
newSpatialRefefence = srFactory.CreateGeographicCoordinateSystem(_wkid);
}

container.Geometry.SpatialReference = newSpatialRefefence;
}

Result = container;
}

private static GeometryContainer CheckForLegacyGeometryInput(GeometryContainer container, string geometryText)
{
var parts = geometryText.Split(':');
if (parts.Length != 2)
{
Result = null;
return;
return null;
}

var ok = true;
container.Type = parts[0].ToUpperInvariant();
_geometryText = parts[1];
geometryText = parts[1];

switch (container.Type)
{
case "POINT":
{
_geometryText = _geometryText.Replace("[", "")
.Replace("]", "")
.Replace(" ", "");
{
geometryText = geometryText.Replace("[", "")
.Replace("]", "")
.Replace(" ", "");

var splits = _geometryText.Split(',');
var coordinates = new Collection<double[]>();
try
{
var point = new double[2]
{
Convert.ToDouble(splits[0]),
Convert.ToDouble(splits[1])
};

coordinates.Add(point);
}
catch (FormatException)
{
_ok = false;
}
catch (OverflowException)
var splits = geometryText.Split(',');
var coordinates = new Collection<double[]>();
try
{
var point = new double[2]
{
_ok = false;
}
Convert.ToDouble(splits[0]),
Convert.ToDouble(splits[1])
};

if (!_ok)
{
Result = null;
return;
}
coordinates.Add(point);
}
catch (FormatException)
{
ok = false;
}
catch (OverflowException)
{
ok = false;
}

if (!ok)
{
return null;
}

container.Coordinates = coordinates;
container.Coordinates = coordinates;

try
{
container.Geometry = new Point
{
X = coordinates[0][0],
Y = coordinates[0][1]
};
}
catch
try
{
container.Geometry = new Point
{
//breaks unit tests since we're not in SOE la la land.
}

break;
X = coordinates[0][0],
Y = coordinates[0][1]
};
}
case "POLYLINE":
catch
{
throw new NotImplementedException("Maybe later");
//breaks unit tests since we're not in SOE la la land.
}

break;
}
case "POLYLINE":
{
throw new NotImplementedException("Maybe later");
}
case "POLYGON":
{
throw new NotImplementedException("Maybe later");
}
{
throw new NotImplementedException("Maybe later");
}
default:
{
Result = null;
return;
}
{
return null;
}
}

Result = container;
return container;
}
}
}
10 changes: 6 additions & 4 deletions WebAPI.Search.Soe/Commands/Factory/SpatialCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ namespace WebAPI.Search.Soe.Commands.Factory
{
public static class SpatialCommandFactory
{
static readonly string[] RasterDatasets = { "RASTER.DEM_10METER" };
public static Command<ResponseContainer<SearchResult>> Get(GeometryContainer container,
IFeatureWorkspace featureWorkspace,
string featureClass, string[] values,
string predicate,
ISpatialReference newSpatialRefefence)
{
if (container == null)
{
return new NonSpatialQueryCommand(featureWorkspace,
new QueryArgs(featureClass, values, predicate, newSpatialRefefence));

var rasterDatasets = new[] { "RASTER.DEM_10METER" };
new QueryArgs(featureClass, values, predicate, newSpatialRefefence));
}

if (rasterDatasets.Any(x => featureClass.ToUpperInvariant().Contains(x)))
if (RasterDatasets.Any(x => featureClass.ToUpperInvariant().Contains(x)))
{
var rasterWorkspace = featureWorkspace as IRasterWorkspaceEx;
return new PointInRasterQueryCommand(rasterWorkspace,
Expand All @@ -33,6 +34,7 @@ public static Command<ResponseContainer<SearchResult>> Get(GeometryContainer co
{
case "POINT":
case "POLYGON":
case "ENVELOPE":
{
return new PointInPolygonQueryCommand(featureWorkspace,
new SpatialQueryArgs(featureClass, container.Geometry,
Expand Down
Loading

0 comments on commit 2839bfe

Please sign in to comment.