Codes for Geometry Generation
Nice work! Would you be kind enough to share the codes for Geometry Generation? I will only use it for academy purpose. Thanks in advance.
On Linux, if you download the cutUSA.tsv file. This requires GNU sed.
sed -i 's/USA\t/, /g' cutUSA.tsv
sed -i '0,/, /{s/, /"type": "FeatureCollection", "features": [/}' cutUSA.tsv
echo "]}" >> cutUSA.tsv
mv cutUSA.tsv cutUSA.geojson
This will give you a proper GeoJSON file to work with. Note that none of the geometries have any properties.
You can then clip the results to a binding box with ogr2ogr.
ogr2ogr.exe -clipsrc -77.05632 38.90080 -77.00172 38.88697 -progress cutUSAClipped.geojson cutUSA.geojson
How to do the same in Windows?
In Python, with the help of ChatGPT, I used the following code. Never worked with a .tsv file before, but it seems to be quite straightforward.
import csv
import json
with open('INPUT.tsv', 'r') as tsv_file:
tsv_reader = csv.reader(tsv_file, delimiter='\t')
data = []
for row in tsv_reader:
data.append(row)
geojson = {
"type": "FeatureCollection",
"features": []
}
for feature in data:
country_code, feature_json = feature[0], feature[1]
feature_dict = json.loads(feature_json)
feature_dict["properties"]["country_code"] = country_code
geojson["features"].append(feature_dict)
with open('output.geojson', 'w') as outfile:
json.dump(geojson, outfile)
Hopefully it works for you too, it worked for me.
Update: I used this code in smaller .tsv files, which worked fine. When I tried to run this in Europe-Full, naturally it took too long. A better idea would be to reduce the size first.
In Python, with the help of ChatGPT, I used the following code. Never worked with a .tsv file before, but it seems to be quite straightforward.
import csv import json with open('INPUT.tsv', 'r') as tsv_file: tsv_reader = csv.reader(tsv_file, delimiter='\t') data = [] for row in tsv_reader: data.append(row) geojson = { "type": "FeatureCollection", "features": [] } for feature in data: country_code, feature_json = feature[0], feature[1] feature_dict = json.loads(feature_json) feature_dict["properties"]["country_code"] = country_code geojson["features"].append(feature_dict) with open('output.geojson', 'w') as outfile: json.dump(geojson, outfile)Hopefully it works for you too, it worked for me.
Update: I used this code in smaller .tsv files, which worked fine. When I tried to run this in Europe-Full, naturally it took too long. A better idea would be to reduce the size first.
thanks timuroeztuerk,
i slightly adapted your code to filter out the country of interest.. works fine..
for row in tsv_reader: if row[0] == "NPL": data.append(row)
For future reference, the code below translates a whole zip file to a local GPKG on your disk. Requires GDAL to be build against SpatialLite >=5.0:
filename=AfricaWest-Full
query="select field_1 as country_code, geomfromgeojson(substr(field_2, 30, length(field_2)-46)) as geometry from \"$filename\""
ogr2ogr -oo SEPARATOR=TAB -oo HEADERS=NO -a_srs EPSG:4326 -nln $filename -dialect sqlite -sql "$query" $filename.gpkg /vsizip//vsicurl/https://usaminedroads.blob.core.windows.net/road-detections/$filename.zip/$filename.tsv
Added a file with country codes as requested in the top comment. Closing this issue