Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust lat/long for Norway? #118

Open
kaimikael opened this issue Dec 8, 2020 · 10 comments
Open

Adjust lat/long for Norway? #118

kaimikael opened this issue Dec 8, 2020 · 10 comments

Comments

@kaimikael
Copy link

Geo version

0.2.8

Orange version

3.27.1

Expected behavior

I think it would make more sense to have the default location of Norway to be in mainland Norway, say somewhere about 62° N, 8° E.

Actual behavior

Currently Norway is represented by a point on Svalbard, which, while admittedly Norwegian territory, is not where you’d look first.

Steps to reproduce the behavior

Show any data containing Norway with Geo Map.

Additional info (worksheets, data, screenshots, ...)

Screenshot 2020-12-08 at 21 31 20

@ajdapretnar
Copy link
Collaborator

I could be that the point is computed as a central point due to Norway having some overseas territories, but unsure if that's the case. I was not able to find where to adjust this. :(

@kaimikael
Copy link
Author

That would be in orange3-geo-master/orangecontrib/geo/geojson/admin1-NOR.json

@ajdapretnar
Copy link
Collaborator

I know, but where in that file? I was only able to find polygon descriptions, couldn't figure out where is the coordinate for the "center of Norway". If you can find it, feel free to submit a pull request. :)

@kaimikael
Copy link
Author

Yes, I was too hasty. There is a lat/long, but it’s for Bouvet Island. Weird. I’ll dig a bit more.

@ajdapretnar
Copy link
Collaborator

Not just Bouvet, also other counties. @kernc Do you perhaps remember if there is a "center" coordinate or it is computed on-the-fly?

@kaimikael
Copy link
Author

Yes, if there isn’t an explicit latitude and longitude given, a “representative point” is computed. So, it should be possible to add lat/long data to the file. I’ll fiddle around and see what I can come up with :-)

@kernc
Copy link
Contributor

kernc commented Dec 9, 2020

What I do remember is that JSONs are generated. Manual edits should probably be made to the generating code. 😅

# Admin1
# NOTE: Admin0 and Admin1 are matched on adm0_a3 field
ogr2ogr -f GeoJSON \
-select name,iso_a2,adm0_a3,adm1_code,fips,admin,code_hasc,type_en,latitude,longitude \
out.json ne_10m_admin_1_states_provinces.shp
sed -i -r 's/"type_en"/"type"/g;
s/"code_hasc"/"hasc"/g;
s/"adm1_code"/"_id"/g;
s/"-1"/null/g;
s/"-99"/null/g;' out.json
simplify .7 out.json admin1.json
rm out.json
python3 -c "
import json
from math import isnan
from itertools import groupby
from collections import OrderedDict
CC = {}
for line in open('iso3166a2a3.txt'):
cc2, cc3 = line.split()
CC[cc3] = cc2
with open('admin1.json') as f:
geo = json.load(f)
def isnull(val):
return (val is None or
val in ('-99', '-1', 'NUL', 'NULL') or
isinstance(val, float) and isnan(val))
out = {'type': 'FeatureCollection',
'features': []}
for feature in geo['features']:
p = feature['properties']
if isnull(p['iso_a2']):
p['iso_a2'] = CC.get(p['adm0_a3'])
out['features'].append(feature)
# with open('admin1.json', 'w') as f:
# json.dump(out, f)
key = lambda feature: feature['properties']['adm0_a3']
for cc, features in groupby(sorted(out['features'], key=key), key):
features = [OrderedDict([
('type', f['type']),
('properties', f['properties']),
('geometry', f['geometry']),
]) for f in features]
print(cc, len(features))
if cc == 'SVN':
from pprint import pprint
pprint(features[0])
with open('admin1-{}.json'.format(cc), 'w') as f:
json.dump(dict(type='FeatureCollection', features=features), f)
"
rm admin1.json
## Alternatively, don't simplify all admin1 at once (above), but simplify
## each country individually here
for file in admin1-*.json; do
simplify .7 "$file" "$file"
done

@kernc
Copy link
Contributor

kernc commented Dec 9, 2020

Then again, maybe lat-longs for admin0 (countries) are computed on-the-fly, in which case the override might also fit somewhere here:

try:
r = shape.representative_point()
except ValueError:
# For GBR, representative point above fails with:
# ValueError: No Shapely geometry can be created from null value
r = shape.centroid
if not r.within(shape):
max_poly = max([shape] if isinstance(shape, Polygon) else list(shape),
key=lambda polygon: polygon.area)
# From https://stackoverflow.com/questions/33311616/find-coordinate-of-closest-point-on-polygon-shapely/33324058#33324058
poly_ext = max_poly.exterior
dist = poly_ext.project(r)
pt = poly_ext.interpolate(dist)
r = Point(pt.coords[0])
assert r.within(shape)
p.update(latitude=r.y, longitude=r.x)

@kaimikael
Copy link
Author

I've been looking at the data files and I can’t figure out how they are generated. As we noted, there is info on Bouvet island (also Norwegian territory) in the admin1-NOR.json file, but nowhere else in the orange3-geo-master tree, so if that file is generated, what is it based on?

@kernc
Copy link
Contributor

kernc commented Dec 9, 2020

if that file is generated, what is it based on?

https://github.com/biolab/orange3-geo/blob/master/CONTRIBUTING.md#building-geojson-files

Based on your screenshot, however, I think you should to be looking at admin0.json and the code snippet above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants