@@ -71,6 +71,7 @@ function displayImportForm() {
7171
7272function openFile ( event ) {
7373 var input = event . target ;
74+
7475 var info_element_id = event . target . id + 'Info'
7576 var content_element_id = event . target . id + 'Content'
7677 var map_element_id = event . target . id + 'Map'
@@ -91,67 +92,77 @@ function openFile(event) {
9192 var zoomLevel = parseInt ( document . getElementById ( 'zoomLevelChangeDetection' ) . value )
9293 }
9394
94- var reader = new FileReader ( ) ;
95- reader . onload = function ( ) {
96-
97- try {
98- var text = reader . result ;
99- var geojsonData = JSON . parse ( text )
100-
101- // check number of features
102- numberOfFeatures = geojsonData [ 'features' ] . length
103- console . log ( 'number of features: ' + numberOfFeatures )
104- if ( numberOfFeatures > 1 ) {
105- throw 'too many features: ' + numberOfFeatures
106- }
107- info_output . innerHTML += 'Number of Features: ' + numberOfFeatures + '<br>' ;
108- info_output . style . display = 'block'
109-
110- // check input geometry type
111- feature = geojsonData [ 'features' ] [ 0 ]
112- type = turf . getType ( feature )
113- console . log ( 'geometry type: ' + type )
114- if ( type !== 'Polygon' & type !== 'MultiPolygon' ) {
115- throw 'wrong geometry type: ' + type
116- }
117- info_output . innerHTML += 'Feature Type: ' + type + '<br>' ;
118- info_output . style . display = 'block'
119-
120- // check project size
121-
122- area = turf . area ( feature ) / 1000000 // area in square kilometers
123- maxArea = ( 20 - zoomLevel ) * ( 20 - zoomLevel ) * 1250
124- console . log ( 'project size: ' + area + ' sqkm' )
125- if ( area > 5000 ) {
126- throw 'project is to large: ' + area + ' sqkm; ' + 'max allowed size for this zoom level: ' + maxArea + ' sqkm'
127- }
128- info_output . innerHTML += 'Project Size: ' + area + ' sqkm<br>' ;
129- info_output . style . display = 'block'
130-
131- // add feature to map
132- layer . clearLayers ( )
133- layer . addData ( geojsonData ) ;
134- map . fitBounds ( layer . getBounds ( ) ) ;
135- console . log ( 'added input geojson feature' )
136-
137- // add text to html object
138- info_output . innerHTML += 'Project seems to be valid :)' ;
139- info_output . style . display = 'block'
140-
141- if ( event . target . id === 'geometry' ) {
142- BuildAreaGeometry = text
143- } else {
144- ChangeDetectionGeometry = text
145- }
146-
147-
148- }
149- catch ( err ) {
150- info_output . innerHTML = '<b>Error reading GeoJSON file</b><br>' + err ;
151- info_output . style . display = 'block'
152- }
153- } ;
95+ // Check file size before loading
96+ var filesize = input . files [ 0 ] . size ;
97+ if ( filesize > 2.5 * 1024 * 1024 ) {
98+ var err = 'filesize is too big (max 2.5MB): ' + filesize / ( 1000 * 1000 )
99+ info_output . innerHTML = '<b>Error reading GeoJSON file</b><br>' + err ;
100+ info_output . style . display = 'block'
101+ } else {
102+ info_output . innerHTML += 'File Size is valid <br>' ;
103+ info_output . style . display = 'block'
104+
105+ var reader = new FileReader ( ) ;
106+ reader . onload = function ( ) {
107+
108+ try {
109+ var text = reader . result ;
110+ var geojsonData = JSON . parse ( text )
111+
112+ // check number of features
113+ numberOfFeatures = geojsonData [ 'features' ] . length
114+ console . log ( 'number of features: ' + numberOfFeatures )
115+ if ( numberOfFeatures > 1 ) {
116+ throw 'too many features: ' + numberOfFeatures
117+ }
118+ info_output . innerHTML += 'Number of Features: ' + numberOfFeatures + '<br>' ;
119+ info_output . style . display = 'block'
120+
121+ // check input geometry type
122+ feature = geojsonData [ 'features' ] [ 0 ]
123+ type = turf . getType ( feature )
124+ console . log ( 'geometry type: ' + type )
125+ if ( type !== 'Polygon' & type !== 'MultiPolygon' ) {
126+ throw 'wrong geometry type: ' + type
127+ }
128+ info_output . innerHTML += 'Feature Type: ' + type + '<br>' ;
129+ info_output . style . display = 'block'
130+
131+ // check project size
132+
133+ area = turf . area ( feature ) / 1000000 // area in square kilometers
134+ maxArea = ( 20 - zoomLevel ) * ( 20 - zoomLevel ) * 1250
135+ console . log ( 'project size: ' + area + ' sqkm' )
136+ if ( area > 5000 ) {
137+ throw 'project is to large: ' + area + ' sqkm; ' + 'max allowed size for this zoom level: ' + maxArea + ' sqkm'
138+ }
139+ info_output . innerHTML += 'Project Size: ' + area + ' sqkm<br>' ;
140+ info_output . style . display = 'block'
141+
142+ // add feature to map
143+ layer . clearLayers ( )
144+ layer . addData ( geojsonData ) ;
145+ map . fitBounds ( layer . getBounds ( ) ) ;
146+ console . log ( 'added input geojson feature' )
147+
148+ // add text to html object
149+ info_output . innerHTML += 'Project seems to be valid :)' ;
150+ info_output . style . display = 'block'
151+
152+ if ( event . target . id === 'geometry' ) {
153+ BuildAreaGeometry = text
154+ } else {
155+ ChangeDetectionGeometry = text
156+ }
157+
158+ }
159+ catch ( err ) {
160+ info_output . innerHTML = '<b>Error reading GeoJSON file</b><br>' + err ;
161+ info_output . style . display = 'block'
162+ }
163+ } ;
154164 reader . readAsText ( input . files [ 0 ] ) ;
165+ }
155166 } ;
156167
157168function openImageFile ( event ) {
0 commit comments