33using System . IO ;
44using System . Linq ;
55using System . Text ;
6+ using System . Text . Json ;
67using System . Text . RegularExpressions ;
78using System . Threading . Tasks ;
89using 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