Skip to content

Commit 090bde9

Browse files
committed
resolve #12
1 parent 01bad72 commit 090bde9

File tree

5 files changed

+152
-11
lines changed

5 files changed

+152
-11
lines changed

RockCollect/RockDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class RockDetector
1616
static public readonly float SHADOW_SPLIT_RATIO = 0.5f;
1717
public const int TILESIZE = 550;
1818

19-
static public readonly float MIN_VALID_GSD = 0;
19+
static public readonly float MIN_VALID_GSD = 0.001f;
2020
static public readonly float MAX_VALID_GSD = 100;
2121

2222
static public readonly float MIN_VALID_AZIMUTH = 0;

RockCollect/Stages/ChooseImage.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,32 @@ public void SetGroundSamplingDistance(float metersPerPixel)
6767
{
6868
GroundSamplingDistance = metersPerPixel;
6969
}
70+
71+
public float GetGroundSamplingDistance()
72+
{
73+
return GroundSamplingDistance;
74+
}
75+
7076
public void SetSubSolarAzimuth(float subsolarAzimuthDegrees)
7177
{
7278
SubSolarAzimuthDegrees = subsolarAzimuthDegrees;
7379
}
7480

81+
public float GetSubSolarAzimuth()
82+
{
83+
return SubSolarAzimuthDegrees;
84+
}
85+
7586
public void SetSolarIncidence(float solarIncidenceDegrees)
7687
{
7788
SolarIncidenceDegrees = solarIncidenceDegrees;
7889
}
7990

91+
public float GetSolarIncidence()
92+
{
93+
return SolarIncidenceDegrees;
94+
}
95+
8096
public void SetComparisonRocklist(string rocklistPath)
8197
{
8298
ComparisonRocklistPath = rocklistPath;
@@ -91,5 +107,52 @@ public void SetShapeFile(string path)
91107
{
92108
ShapeFilePath = path;
93109
}
110+
111+
public override bool Deactivate(bool forward)
112+
{
113+
string msg = null;
114+
if (string.IsNullOrEmpty(ImagePath))
115+
{
116+
msg = "No image file selected.";
117+
}
118+
else if (!File.Exists(ImagePath))
119+
{
120+
msg = string.Format("Image \"{0}\" not found.", ImagePath);
121+
}
122+
else if (!string.IsNullOrEmpty(ShapeFilePath) && !File.Exists(ShapeFilePath))
123+
{
124+
msg = string.Format("Shape file \"{0}\" not found.", ShapeFilePath);
125+
}
126+
else if (!string.IsNullOrEmpty(ComparisonRocklistPath) && !File.Exists(ComparisonRocklistPath))
127+
{
128+
msg = string.Format("Comparison rock list \"{0}\" not found.", ComparisonRocklistPath);
129+
}
130+
else if (GroundSamplingDistance < RockDetector.MIN_VALID_GSD ||
131+
GroundSamplingDistance > RockDetector.MAX_VALID_GSD)
132+
{
133+
msg = string.Format("Ground sampling distance {0} not in range {1} to {2}.", GroundSamplingDistance,
134+
RockDetector.MIN_VALID_GSD, RockDetector.MAX_VALID_GSD);
135+
}
136+
else if (SubSolarAzimuthDegrees < RockDetector.MIN_VALID_AZIMUTH ||
137+
SubSolarAzimuthDegrees > RockDetector.MAX_VALID_AZIMUTH)
138+
{
139+
msg = string.Format("Sub-solar azimuth angle {0} not in range {1} to {2}.", SubSolarAzimuthDegrees,
140+
RockDetector.MIN_VALID_AZIMUTH, RockDetector.MAX_VALID_AZIMUTH);
141+
}
142+
else if (SolarIncidenceDegrees < RockDetector.MIN_VALID_INCIDENCE ||
143+
SolarIncidenceDegrees > RockDetector.MAX_VALID_INCIDENCE)
144+
{
145+
msg = string.Format("Solar incidence angle {0} not in range {1} to {2}.", SolarIncidenceDegrees,
146+
RockDetector.MIN_VALID_INCIDENCE, RockDetector.MAX_VALID_INCIDENCE);
147+
}
148+
149+
if (msg != null)
150+
{
151+
MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
152+
return false;
153+
}
154+
155+
return base.Deactivate(forward);
156+
}
94157
}
95158
}

RockCollect/Stages/ChooseImageUI.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ private void buttonAutoFillFromComparisonRocklist_Click(object sender, EventArgs
144144
numericGSD.Value = (decimal)rocklist.paramList.GSD_resolution;
145145
numericIncidence.Value = (decimal)rocklist.paramList.sun_incidence_angle;
146146
numericAzimuth.Value = (decimal)rocklist.paramList.sun_azimuth_angle;
147+
Stage.SetGroundSamplingDistance(rocklist.paramList.GSD_resolution);
148+
Stage.SetSolarIncidence(rocklist.paramList.sun_incidence_angle);
149+
Stage.SetSubSolarAzimuth(rocklist.paramList.sun_azimuth_angle);
147150
}
148151
catch
149152
{
@@ -288,6 +291,10 @@ private void buttonAutoFillFromEDRIndex_Click(object sender, EventArgs e)
288291
numericGSD.Value = (decimal)gsd;
289292
numericIncidence.Value = (decimal)incidence;
290293
numericAzimuth.Value = (decimal)azimuth;
294+
295+
Stage.SetGroundSamplingDistance(gsd);
296+
Stage.SetSolarIncidence(incidence);
297+
Stage.SetSubSolarAzimuth(azimuth);
291298
}
292299

293300
private void buttonShapeFile_Click(object sender, EventArgs e)

RockCollect/Stages/TileSelect.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ void RockListToShapeFile(string path)
11111111
}
11121112
}
11131113

1114+
if (gsd < RockDetector.MIN_VALID_GSD || gsd > RockDetector.MAX_VALID_GSD)
1115+
{
1116+
throw new Exception(string.Format("Invalid ground sampling distance {0} not in range {1} to {2}",
1117+
gsd, RockDetector.MIN_VALID_GSD, RockDetector.MAX_VALID_GSD));
1118+
}
1119+
11141120
Console.WriteLine(string.Format("Loaded rock list {0} with {1} rocks, gsd={2}", path, rocks.Count(), gsd));
11151121

11161122
if (!foundColumnHeader)

RockCollect/Workflow.cs

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using System.Text.Json;
67
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89
using System.Windows.Forms;
@@ -93,13 +94,13 @@ public void NextStage()
9394
{
9495
Regex tileRegex = new Regex(@"^Tile_\d+_\d+.json$");
9596

96-
int numExisting = Directory.GetFiles(FinalOutputDirectory)
97+
List<string> existing = Directory.GetFiles(FinalOutputDirectory)
9798
.Where(path => tileRegex.IsMatch(Path.GetFileName(path)))
98-
.Count();
99+
.ToList();
99100

100-
Console.WriteLine("found {0} existing tile settings", numExisting);
101+
Console.WriteLine("found {0} existing tile settings", existing.Count);
101102

102-
if (numExisting > 0)
103+
if (existing.Count > 0)
103104
{
104105
string savePath = null;
105106
for (int i = 0; savePath == null; i++)
@@ -108,12 +109,63 @@ public void NextStage()
108109
if (Directory.Exists(savePath) || File.Exists(savePath)) savePath = null;
109110
}
110111

111-
DialogResult result =
112-
MessageBox.Show(string.Format("Use {0} existing tile settings at {1}? " +
113-
"If not they will be moved to {2}.",
114-
numExisting, FinalOutputDirectory, savePath),
115-
"Use Existing Tiles", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
116-
if (result == DialogResult.No)
112+
//verify that all existing GSD, AZIMUTH, and INCIDENCE equal the values set in ChooseImage
113+
//TileSelect.cs will separately check for invalid or partial tile settings
114+
var ci = activeStage as RockCollect.Stages.ChooseImage;
115+
float gsd = ci.GetGroundSamplingDistance();
116+
float azimuth = ci.GetSubSolarAzimuth();
117+
float incidence = ci.GetSolarIncidence();
118+
string msg = null;
119+
foreach (string file in existing)
120+
{
121+
try
122+
{
123+
var data = JsonSerializer.Deserialize<StageData>(File.ReadAllText(file));
124+
string reason = CheckFloat(data.Data, "GSD", gsd);
125+
if (reason == null)
126+
{
127+
reason = CheckFloat(data.Data, "AZIMUTH", azimuth);
128+
}
129+
if (reason == null)
130+
{
131+
reason = CheckFloat(data.Data, "INCIDENCE", incidence);
132+
}
133+
if (reason != null)
134+
{
135+
msg = string.Format("{0} in {1}", reason, file);
136+
break;
137+
}
138+
}
139+
catch (Exception ex)
140+
{
141+
msg = string.Format("error parsing JSON {0}: {1}", file, ex.Message);
142+
break;
143+
}
144+
}
145+
146+
bool move = false;
147+
if (msg != null)
148+
{
149+
MessageBox.Show(
150+
string.Format("Existing tile settings at {0} contain invalid settings: {1}. " +
151+
"Moving them to {2}.", FinalOutputDirectory, msg, savePath),
152+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
153+
move = true;
154+
}
155+
else
156+
{
157+
DialogResult result =
158+
MessageBox.Show(string.Format("Use {0} existing tile settings at {1}? " +
159+
"If not they will be moved to {2}.",
160+
existing.Count, FinalOutputDirectory, savePath),
161+
"Use Existing Tiles", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
162+
if (result == DialogResult.No)
163+
{
164+
move = true;
165+
}
166+
}
167+
168+
if (move)
117169
{
118170
Directory.Move(FinalOutputDirectory, savePath);
119171
Directory.CreateDirectory(FinalOutputDirectory);
@@ -171,5 +223,18 @@ public void PreviousStage()
171223
nextStage.Activate(WorkArea, StatusForm, false);
172224
}
173225
}
226+
227+
private static string CheckFloat(Dictionary<string, string> strings, string key, float expected)
228+
{
229+
if (!strings.ContainsKey(key)) return $"{key} not present";
230+
231+
float v;
232+
try { v = float.Parse(strings[key]); }
233+
catch (FormatException) { return $"value \"{strings[key]}\" for {key} not a valid float"; }
234+
235+
if (v == expected) return null;
236+
237+
return $"expected {expected} for {key}, got {v}";
238+
}
174239
}
175240
}

0 commit comments

Comments
 (0)