GeoJSON is great! It can be used in the web maps, can be used as a data exchange format especially with Web Feature Service (WFS) and much more. However, sometimes the white space in the GeoJSON file can add up to the size of the file, as a result of which it becomes slower in rendering. Use the following code to remove your extra white spaces and save some spaces.
Make sure you have installed the
jsmin module. You can install them as
pip install jsmin. Have all your GeoJSON files in a folder called
geojson or change the name accordingly.
from jsmin import jsmin import os foldername = 'geojson' os.path.dirname(os.path.dirname(__file__)) path = os.path.join(os.getcwd(), foldername) path = os.path.abspath(path) files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] for file in files: print('started file: %s' % file) file_path = os.path.join(os.getcwd(), foldername, file) with open(file_path, 'r+') as js_file: minified = jsmin(js_file.read()) js_file.seek(0) js_file.truncate() js_file.write(minified) js_file.close() print('end file: %s' % file) print('**********************')
Leave a Reply