Skip to content

Commit

Permalink
Revert "Revert "Added transforms for HeronSRS and new DownloadHttpIma…
Browse files Browse the repository at this point in the history
…ge functionality""

This reverts commit c13b382.
  • Loading branch information
blueherongis committed Jul 31, 2022
1 parent abb28eb commit cc553ec
Show file tree
Hide file tree
Showing 13 changed files with 1,719 additions and 119 deletions.
18 changes: 16 additions & 2 deletions Heron/Components/GIS Import-Export/ExportVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,24 @@ protected override void SolveInstance(IGH_DataAccess DA)
///Using geojson as a flexiblle base file type which can be converted later with ogr2ogr
DataSource ds = drv.CreateDataSource("/vsimem/out.geojson", null);

///Get HeronSRS
OSGeo.OSR.SpatialReference heronSRS = new OSGeo.OSR.SpatialReference("");
heronSRS.SetFromUserInput(HeronSRS.Instance.SRS);
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Heron's Spatial Spatial Reference System (SRS): " + HeronSRS.Instance.SRS);
int heronSRSInt = Int16.Parse(heronSRS.GetAuthorityCode(null));
Message = "EPSG:" + heronSRSInt;

///Apply EAP to HeronSRS
Transform heronToUserSRSTransform = Heron.Convert.GetUserSRSToHeronSRSTransform(heronSRS);
Transform userSRSToHeronTransform = Heron.Convert.GetHeronSRSToUserSRSTransform(heronSRS);


///Use WGS84 spatial reference
OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference("");
dst.SetWellKnownGeogCS("WGS84");
Transform transform = Heron.Convert.XYZToWGSTransform();
dst.SetFromUserInput(HeronSRS.Instance.SRS);
//Transform transform = Heron.Convert.XYZToWGSTransform();
Transform transform = Heron.Convert.GetHeronSRSToUserSRSTransform(heronSRS);


///Use OGR catch-all for geometry types
var gtype = wkbGeometryType.wkbGeometryCollection;
Expand Down
79 changes: 71 additions & 8 deletions Heron/Components/GIS Import-Export/ImportOSM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ protected override void SolveInstance(IGH_DataAccess DA)
Transform xformToMetric = new Transform(scaleToMetric);
Transform xformFromMetric = new Transform(scaleFromMetric);

///GDAL setup
RESTful.GdalConfiguration.ConfigureOgr();
RESTful.GdalConfiguration.ConfigureGdal();

///Set transform from input spatial reference to Heron spatial reference
OSGeo.OSR.SpatialReference heronSRS = new OSGeo.OSR.SpatialReference("");
heronSRS.SetFromUserInput(HeronSRS.Instance.SRS);
OSGeo.OSR.SpatialReference osmSRS = new OSGeo.OSR.SpatialReference("");
osmSRS.SetFromUserInput("WGS84");
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Heron's Spatial Spatial Reference System (SRS): " + HeronSRS.Instance.SRS);
int heronSRSInt = Int16.Parse(heronSRS.GetAuthorityCode(null));
Message = "EPSG:" + heronSRSInt;

///Apply EAP to HeronSRS
Transform userSRSToModelTransform = Heron.Convert.GetUserSRSToHeronSRSTransform(heronSRS);
Transform heronToUserSRSTransform = Heron.Convert.GetHeronSRSToUserSRSTransform(heronSRS);

///Set transforms between source and HeronSRS
OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(osmSRS, heronSRS);
OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(heronSRS, osmSRS);


///Declare trees
Rectangle3d recs = new Rectangle3d();
GH_Structure<GH_String> fieldNames = new GH_Structure<GH_String>();
Expand All @@ -94,10 +116,12 @@ protected override void SolveInstance(IGH_DataAccess DA)
if (boundary != null)
{
Point3d maxM = boundary.GetBoundingBox(true).Corner(true, false, true);
max = Heron.Convert.XYZToWGS(maxM);
maxM.Transform(heronToUserSRSTransform);
max = Heron.Convert.OSRTransformPoint3dToPoint3d(maxM, revTransform);

Point3d minM = boundary.GetBoundingBox(true).Corner(false, true, true);
min = Heron.Convert.XYZToWGS(minM);
minM.Transform(heronToUserSRSTransform);
min = Heron.Convert.OSRTransformPoint3dToPoint3d(minM, revTransform);
}

/// get extents (why is this not part of OsmSharp?)
Expand All @@ -108,8 +132,10 @@ protected override void SolveInstance(IGH_DataAccess DA)
double minlon = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("minlon").Value);
double maxlat = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("maxlat").Value);
double maxlon = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("maxlon").Value);
Point3d boundsMin = Heron.Convert.WGSToXYZ(new Point3d(minlon, minlat, 0));
Point3d boundsMax = Heron.Convert.WGSToXYZ(new Point3d(maxlon, maxlat, 0));
Point3d boundsMin = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d(minlon, minlat, 0), coordTransform);
boundsMin.Transform(userSRSToModelTransform);
Point3d boundsMax = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d(maxlon, maxlat, 0), coordTransform);
boundsMax.Transform(userSRSToModelTransform);

recs = new Rectangle3d(Plane.WorldXY, boundsMin, boundsMax);
}
Expand Down Expand Up @@ -180,7 +206,9 @@ where osmGeos.Tags.Intersect(tags).Any()
fieldValues.AppendRange(GetValues(osmGeo), nodesPath);

//get geometry for node
Point3d nPoint = Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0));
//Point3d nPoint = Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0));
Point3d nPoint = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d((double)n.Longitude, (double)n.Latitude, 0), coordTransform);
nPoint.Transform(userSRSToModelTransform);
geometryGoo.Append(new GH_Point(nPoint), nodesPath);

//increment nodes
Expand All @@ -203,7 +231,11 @@ where osmGeos.Tags.Intersect(tags).Any()
foreach (long j in w.Nodes)
{
OsmSharp.Node n = (OsmSharp.Node)sourceMem.Get(OsmGeoType.Node, j);
wayNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0)));
//wayNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0)));
Point3d nPt = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d((double)n.Longitude, (double)n.Latitude, 0), coordTransform);
nPt.Transform(userSRSToModelTransform);
wayNodes.Add(nPt);

}

PolylineCurve pL = new PolylineCurve(wayNodes);
Expand Down Expand Up @@ -290,7 +322,9 @@ where osmGeos.Tags.Intersect(tags).Any()
{
long memNodeId = rMem.Id;
OsmSharp.Node memN = (OsmSharp.Node)sourceMem.Get(rMem.Type, rMem.Id);
Point3d memPoint = Heron.Convert.WGSToXYZ(new Point3d((double)memN.Longitude, (double)memN.Latitude, 0));
//Point3d memPoint = Heron.Convert.WGSToXYZ(new Point3d((double)memN.Longitude, (double)memN.Latitude, 0));
Point3d memPoint = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d((double)memN.Longitude, (double)memN.Latitude, 0), coordTransform);
memPoint.Transform(userSRSToModelTransform);
geometryGoo.Append(new GH_Point(memPoint), memberPath);
}

Expand All @@ -306,7 +340,10 @@ where osmGeos.Tags.Intersect(tags).Any()
foreach (long memNodeId in memWay.Nodes)
{
OsmSharp.Node memNode = (OsmSharp.Node)sourceMem.Get(OsmGeoType.Node, memNodeId);
memNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)memNode.Longitude, (double)memNode.Latitude, 0)));
//memNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)memNode.Longitude, (double)memNode.Latitude, 0)));
Point3d memPt = Heron.Convert.OSRTransformPoint3dToPoint3d(new Point3d((double)memNode.Longitude, (double)memNode.Latitude, 0), coordTransform);
memPt.Transform(userSRSToModelTransform);
memNodes.Add(memPt);
}

PolylineCurve memPolyline = new PolylineCurve(memNodes);
Expand All @@ -331,6 +368,8 @@ where osmGeos.Tags.Intersect(tags).Any()
//end members loop

bool allClosed = true;
var pLinesJoined = Curve.JoinCurves(pLines); ///try to join ways that may not already be closed ie SF City Hall
pLines = pLinesJoined.ToList();
foreach (Curve pc in pLines)
{
if (!pc.IsClosed)
Expand Down Expand Up @@ -525,6 +564,30 @@ public static IGH_GeometricGoo BldgPartToRoof(BuildingPart bldgPart)
if (pyramidBrep[0].IsSolid) { roof = GH_Convert.ToGeometricGoo(pyramidBrep[0]); }
break;

case "dome":
double domeHeight = centroid.DistanceTo(pL.PointAtStart);
double baseHeight = height - min_height - roofHeight + (roofHeight - domeHeight);

var topArc = new Point3d (centroid.X,centroid.Y,height);
var bottomArc = new Point3d(pL.PointAtStart.X, pL.PointAtStart.Y, pL.PointAtStart.Z + baseHeight);

Arc arc = new Arc(bottomArc, Vector3d.ZAxis, topArc);

if (baseHeight > 0)
{
Line podiumLine = new Line(pL.PointAtStart, bottomArc);
Curve revCurve = Curve.JoinCurves(new List<Curve>() { podiumLine.ToNurbsCurve(), arc.ToNurbsCurve() })[0];
var sweep = RevSurface.Create(revCurve, new Line(centroid, topArc));
roof = GH_Convert.ToGeometricGoo(sweep.ToBrep().CapPlanarHoles(DocumentTolerance()));
}
else
{
var sweep = RevSurface.Create(arc.ToNurbsCurve(), new Line(centroid, topArc));
roof = GH_Convert.ToGeometricGoo(sweep.ToBrep().CapPlanarHoles(DocumentTolerance()));
}

break;

case "skillion":
Line frontEdge = new Line();
Line backEdge = new Line();
Expand Down
39 changes: 27 additions & 12 deletions Heron/Components/GIS Import-Export/ImportRaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,21 @@ protected override void SolveInstance(IGH_DataAccess DA)
};
srcInfo = Gdal.GDALInfo(datasource, new GDALInfoOptions(infoOptions.ToArray()));

//OSGeo.OSR.SpatialReference sr = new SpatialReference(ds.GetProjection());
OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference("");
dst.SetWellKnownGeogCS("WGS84");
OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, dst);
OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(dst, sr);
///Get HeronSRS
OSGeo.OSR.SpatialReference heronSRS = new OSGeo.OSR.SpatialReference("");
heronSRS.SetFromUserInput(HeronSRS.Instance.SRS);
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Heron's Spatial Spatial Reference System (SRS): " + HeronSRS.Instance.SRS);
int heronSRSInt = Int16.Parse(heronSRS.GetAuthorityCode(null));
Message = "EPSG:" + heronSRSInt;

///Apply EAP to HeronSRS
Transform heronToUserSRSTransform = Heron.Convert.GetUserSRSToHeronSRSTransform(heronSRS);
Transform userSRSToHeronTransform = Heron.Convert.GetHeronSRSToUserSRSTransform(heronSRS);

//OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference("");
//dst.SetWellKnownGeogCS("WGS84");
OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, heronSRS);
OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(heronSRS, sr);

double[] adfGeoTransform = new double[6];
double[] invTransform = new double[6];
Expand All @@ -151,15 +161,17 @@ protected override void SolveInstance(IGH_DataAccess DA)
Point3d dsMax = new Point3d(extMaxPT[0], extMaxPT[1], extMaxPT[2]);

///Get bounding box for entire raster data
Rectangle3d datasourceBBox = new Rectangle3d(Plane.WorldXY, Heron.Convert.WGSToXYZ(dsMin), Heron.Convert.WGSToXYZ(dsMax));

//Rectangle3d datasourceBBox = new Rectangle3d(Plane.WorldXY, Heron.Convert.WGSToXYZ(dsMin), Heron.Convert.WGSToXYZ(dsMax));
dsMin.Transform(heronToUserSRSTransform);
dsMax.Transform(heronToUserSRSTransform);
Rectangle3d datasourceBBox = new Rectangle3d(Plane.WorldXY, dsMin, dsMax);

///https://gis.stackexchange.com/questions/312440/gdal-translate-bilinear-interpolation
///set output to georeferenced tiff as a catch-all

string clippedRasterFile = clippedLocation + Path.GetFileNameWithoutExtension(sourceFileLocation) + "_clipped.tif";
string previewPNG = clippedLocation + Path.GetFileNameWithoutExtension(sourceFileLocation) + "_preview.png";
if (sourceFileLocation.Contains("http"))
if (sourceFileLocation.Contains("http://") || sourceFileLocation.Contains("https://"))
{
previewPNG = clippedLocation + "ImportRaster_preview.png";
clippedRasterFile = clippedLocation + "ImportRaster_clipped.tif";
Expand All @@ -170,8 +182,11 @@ protected override void SolveInstance(IGH_DataAccess DA)
if (boundary != null)
{

Point3d clipperMin = Heron.Convert.XYZToWGS(boundary.GetBoundingBox(true).Corner(true, false, true));
Point3d clipperMax = Heron.Convert.XYZToWGS(boundary.GetBoundingBox(true).Corner(false, true, true));
Point3d clipperMin = boundary.GetBoundingBox(true).Corner(true, false, true);
Point3d clipperMax = boundary.GetBoundingBox(true).Corner(false, true, true);
clipperMin.Transform(userSRSToHeronTransform);
clipperMax.Transform(userSRSToHeronTransform);


double lonWest = clipperMin.X;
double lonEast = clipperMax.X;
Expand All @@ -183,7 +198,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
{
"-of", "GTiff",
//"-a_nodata", "0",
"-projwin_srs", "WGS84",
"-projwin_srs", HeronSRS.Instance.SRS,
"-projwin", $"{lonWest}", $"{latNorth}", $"{lonEast}", $"{latSouth}"
};

Expand Down
Loading

2 comments on commit cc553ec

@blueherongis
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit got messy. Accidentally committed a bunch of changes all at once. Tried reverting (because I don't really know what I'm doing) and that was the wrong thing because it lost a bunch of changes. So I reverted the revert to bring them back. The lesson here is to commit more often.

@blueherongis
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes that were included in this commit that weren't documented in the commit name:
Export Vector - changed to work with HeronSRS
ImportOSM - changed to work with HeronSRS; added dome functionality for roofs
ImportRaster - changed to work with HeronSRS
ImportVectorSRS - updated ImportVector to work with HeronSRS
RESTRasterSRS - updated RESTRaster to work with HeronSRS; query now sets width and height separately so the returned image conforms more to the boundary
RESTRevGeo - changed to work with HeronSRS
RESTOSM - changed to work with HeronSRS
RESTVectorSRS - updated RESTVector to work with HeronSRS
DDtoXY - changed to work with HeronSRS
XYtoDD - changed to work with HeronSRS
Convert - added transforms to work with HeronSRS and DownloadHttpImage functionality

Please sign in to comment.