RoadDetections icon indicating copy to clipboard operation
RoadDetections copied to clipboard

Codes for Geometry Generation

Open lukx1994 opened this issue 3 years ago • 6 comments

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.

lukx1994 avatar Dec 12 '22 09:12 lukx1994

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

maxolasersquad avatar Dec 27 '22 18:12 maxolasersquad

How to do the same in Windows?

rajroy367 avatar Dec 28 '22 18:12 rajroy367

Use MinGW or WSL. If equivalent commands exist for native Windows, I'm not familiar with them.

maxolasersquad avatar Dec 28 '22 19:12 maxolasersquad

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.

timuroeztuerk avatar Dec 29 '22 10:12 timuroeztuerk

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)

sukuchha avatar Jan 01 '23 05:01 sukuchha

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

goergen95 avatar Jan 16 '24 10:01 goergen95

Added a file with country codes as requested in the top comment. Closing this issue