0

I need help with the following line of code I found in GitHub: Land Use Land Cover

DATA_FOLDER = os.path.join('..', '..', 'example_data')

I don't know what to put between '.', plus I only downloaded svn_buffered.geojson file. and this is its current directory: C:\Users\ASUS\Desktop\PFE-Master\Code I don't get why do I need to concatenate several paths.

This is the full code:

# Folder where data for running the notebook is stored DATA_FOLDER = os.path.join('..', '..', 'example_data') # Load geojson file country = gpd.read_file(os.path.join(DATA_FOLDER, 'svn_buffered.geojson')) # Convert CRS to UTM_33N country_crs = CRS.UTM_33N country = country.to_crs(crs=country_crs.pyproj_crs()) # Get the country's shape in polygon format country_shape = country.geometry.tolist()[-1] # Plot country country.plot() plt.axis('off'); # Print size print('Dimension of the area is {0:.0f} x {1:.0f} m2'.format(country_shape.bounds[2] - country_shape.bounds[0], country_shape.bounds[3] - country_shape.bounds[1]))``` 
2
  • You need that to go through parent directories. You don't put anything between '.' Commented May 7, 2020 at 23:08
  • could you please elaborate on that? Commented May 7, 2020 at 23:12

1 Answer 1

1

The script you linked is SI_LULC_pipeline.ipynb and it sits in the project at eo-learn/examples/land-cover-map/SI_LULC_pipeline.ipynb.

Since it is trying to access data in eo-learn/example_data/, to get to the data from the working directory of the script (which apparently is the folder it sits in), it would need to access ../../example_data on almost every OS in the world and the web or ..\..\example_data on Windows.

To avoid using one OS convention or the other, the author of the script has kept it clean and calls os.path.join('..', '..', 'example_data') instead, which leaves it up to Python to decide whether to use '/' or '\' to separate the parts of the path (or whatever the symbol is on the OS it runs on).

In case the '..' itself confuses you: .. means 'the parent directory of the current directory'. Any path either starts in the root (if it begins with \ or /) or in the current working directory of the script. To get access to a directory relative to the current working directory, but via a parent directory, you use ... Similarly, . refers to the current working directory and you can use that at the start of a path to make it explicitly relative to the working directory.

Note how https://github.com/sentinel-hub/eo-learn/blob/master/example_data/svn_buffered.geojson sits in master/example_data/ - compare that to the path the script itself sits in master/examples/land-cover-map. To get from the script to the data, you need to go to the parent folder, then to the parent folder again and then into example_data.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I am not sure if I am following, but in case I download the data and replicate the script, how can I access the file, given that it sits alongside the Jupyter notebook in Code file (directory: C:\Users\ASUS\Desktop\PFE-Master\Code)
If you change the structure of the project after downloading, you'll have to change the path to match your local situation. If the .json sits right next to the .py, you can just remove the '..' parts. However, it's recommended not to change the structure of an example project - you'd be better off just getting git and cloning the project; sounds more complicated than it is and it will save you a lot of pointless bug-hunting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.