diff --git a/.gitignore b/.gitignore index 6b921e4e5..1abb140d1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__ local_settings.py data/* !data/accessibility_rules.csv +!data/geometry .idea .coverage venv_smbackend diff --git a/data/geometry/koirametsa.geojson b/data/geometry/koirametsa.geojson new file mode 100644 index 000000000..1f890129d --- /dev/null +++ b/data/geometry/koirametsa.geojson @@ -0,0 +1,645 @@ +{ + "type": "FeatureCollection", + "name": "koirametsa", + "crs": { + "type": "name", + "properties": { + "name": "urn:ogc:def:crs:EPSG::3879" + } + }, + "features": [ + { + "type": "Feature", + "properties": { + "osan_id": 469828.0 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 25512272.3997, + 6684215.00760000012815 + ], + [ + 25512279.781800001859665, + 6684226.996399999596179 + ], + [ + 25512359.0548, + 6684244.696000000461936 + ], + [ + 25512393.0704, + 6684265.381699999794364 + ], + [ + 25512519.044599998742342, + 6684296.174100000411272 + ], + [ + 25512531.768199998885393, + 6684304.281899999827147 + ], + [ + 25512580.881099998950958, + 6684318.138299999758601 + ], + [ + 25512669.6631, + 6684336.393699999898672 + ], + [ + 25512708.1263, + 6684353.6118 + ], + [ + 25512773.894000001251698, + 6684371.015999999828637 + ], + [ + 25512888.2126, + 6684398.184899999760091 + ], + [ + 25512891.2738, + 6684397.700199999846518 + ], + [ + 25512890.036899998784065, + 6684395.941999999806285 + ], + [ + 25512878.7216, + 6684385.163599999621511 + ], + [ + 25512867.8345, + 6684378.749800000339746 + ], + [ + 25512850.173700001090765, + 6684370.89809999987483 + ], + [ + 25512821.3310999982059, + 6684362.482200000435114 + ], + [ + 25512807.712699998170137, + 6684357.047399999573827 + ], + [ + 25512796.4657, + 6684351.004300000146031 + ], + [ + 25512784.415199998766184, + 6684342.299200000241399 + ], + [ + 25512772.9128, + 6684330.466799999587238 + ], + [ + 25512765.4688, + 6684321.684899999760091 + ], + [ + 25512758.2566, + 6684311.872899999842048 + ], + [ + 25512749.546300001442432, + 6684298.157100000418723 + ], + [ + 25512740.936099998652935, + 6684281.64879999961704 + ], + [ + 25512732.6191, + 6684254.614299999549985 + ], + [ + 25512724.371599998325109, + 6684221.855200000107288 + ], + [ + 25512715.9677, + 6684189.068099999800324 + ], + [ + 25512710.442699998617172, + 6684171.608199999667704 + ], + [ + 25512709.18470000103116, + 6684167.63300000037998 + ], + [ + 25512703.116, + 6684151.343100000172853 + ], + [ + 25512692.8422, + 6684120.706899999640882 + ], + [ + 25512689.7311, + 6684109.275299999862909 + ], + [ + 25512687.9525, + 6684102.7401 + ], + [ + 25512678.5746, + 6684058.895999999716878 + ], + [ + 25512657.046, + 6683977.3202 + ], + [ + 25512624.843199998140335, + 6683857.479399999603629 + ], + [ + 25512607.687399998307228, + 6683793.005900000222027 + ], + [ + 25512606.3134, + 6683787.48340000025928 + ], + [ + 25512605.0086, + 6683782.238699999637902 + ], + [ + 25512604.7647, + 6683781.258399999700487 + ], + [ + 25512599.9228, + 6683761.861200000159442 + ], + [ + 25512597.660100001841784, + 6683762.451100000180304 + ], + [ + 25512564.2982, + 6683701.663200000301003 + ], + [ + 25512562.032000001519918, + 6683697.534099999815226 + ], + [ + 25512528.535, + 6683636.500199999660254 + ], + [ + 25512459.90989999845624, + 6683653.778300000354648 + ], + [ + 25512464.1968, + 6683669.305800000205636 + ], + [ + 25512464.350900001823902, + 6683669.864 + ], + [ + 25512465.6939, + 6683674.729000000283122 + ], + [ + 25512099.5588, + 6683425.12459999974817 + ], + [ + 25512082.8466, + 6683413.7314 + ], + [ + 25512037.324299998581409, + 6683382.697599999606609 + ], + [ + 25512038.25560000166297, + 6683381.069799999706447 + ], + [ + 25512030.4073, + 6683375.721599999815226 + ], + [ + 25512028.9084, + 6683378.380599999800324 + ], + [ + 25511938.737300001084805, + 6683315.24129999987781 + ], + [ + 25511835.288199998438358, + 6683288.8141 + ], + [ + 25511832.283300001174212, + 6683288.198900000192225 + ], + [ + 25511830.3251, + 6683288.943 + ], + [ + 25511854.5526, + 6683369.7056 + ], + [ + 25511819.0194, + 6683466.43059999961406 + ], + [ + 25511810.6404, + 6683503.848500000312924 + ], + [ + 25511808.05180000141263, + 6683541.442099999636412 + ], + [ + 25511813.657200001180172, + 6683567.378899999894202 + ], + [ + 25511812.305599998682737, + 6683611.962500000372529 + ], + [ + 25511804.7901, + 6683642.079199999570847 + ], + [ + 25511801.3473, + 6683658.847699999809265 + ], + [ + 25511799.362, + 6683668.875300000421703 + ], + [ + 25511795.048099998384714, + 6683680.061300000175834 + ], + [ + 25511778.1637, + 6683697.7555 + ], + [ + 25511755.371599998325109, + 6683723.47829999960959 + ], + [ + 25511746.5801, + 6683741.263799999840558 + ], + [ + 25511733.521200001239777, + 6683789.254300000146031 + ], + [ + 25511729.202799998223782, + 6683812.85950000025332 + ], + [ + 25511729.8321, + 6683831.535000000149012 + ], + [ + 25511730.0685, + 6683831.882299999706447 + ], + [ + 25511776.9179, + 6683880.026100000366569 + ], + [ + 25511803.02760000154376, + 6683906.0115 + ], + [ + 25511796.895599998533726, + 6683877.685499999672174 + ], + [ + 25511807.7432, + 6683875.3372 + ], + [ + 25511814.6673, + 6683872.50789999961853 + ], + [ + 25511819.452100001275539, + 6683865.05489999987185 + ], + [ + 25511820.1538, + 6683854.438799999654293 + ], + [ + 25511821.154599998146296, + 6683846.491100000217557 + ], + [ + 25511823.535500001162291, + 6683843.1442 + ], + [ + 25511826.9052, + 6683841.830299999564886 + ], + [ + 25511847.5962, + 6683890.794599999673665 + ], + [ + 25511859.144000001251698, + 6683896.650600000284612 + ], + [ + 25511868.494600001722574, + 6683914.029699999839067 + ], + [ + 25511876.2582, + 6683921.103400000371039 + ], + [ + 25511896.2595, + 6683925.5088 + ], + [ + 25511900.9688, + 6683929.528300000354648 + ], + [ + 25511905.3136, + 6683944.011400000192225 + ], + [ + 25511967.0401, + 6683999.0362 + ], + [ + 25511983.1483, + 6684011.218299999833107 + ], + [ + 25511998.468199998140335, + 6684014.340400000102818 + ], + [ + 25512013.3374, + 6684014.791199999861419 + ], + [ + 25512029.02760000154376, + 6684005.699300000444055 + ], + [ + 25512060.0544, + 6683946.57660000026226 + ], + [ + 25512061.038600001484156, + 6683929.335900000296533 + ], + [ + 25512058.752099998295307, + 6683924.393899999558926 + ], + [ + 25512038.4883, + 6683909.660400000400841 + ], + [ + 25512037.822, + 6683906.164800000376999 + ], + [ + 25512065.8237, + 6683870.304100000299513 + ], + [ + 25512100.761, + 6683825.561300000175834 + ], + [ + 25512109.317899998277426, + 6683825.345700000412762 + ], + [ + 25512161.607500001788139, + 6683871.107599999755621 + ], + [ + 25512161.9814000017941, + 6683874.4441 + ], + [ + 25512151.299600001424551, + 6683889.278900000266731 + ], + [ + 25512063.096, + 6684014.4527000002563 + ], + [ + 25512043.0396, + 6684069.491899999789894 + ], + [ + 25512048.7038, + 6684093.051099999807775 + ], + [ + 25512131.5214, + 6684143.341300000436604 + ], + [ + 25512145.9289, + 6684146.874200000427663 + ], + [ + 25512218.826499998569489, + 6684139.910000000149012 + ], + [ + 25512241.4364, + 6684130.496299999766052 + ], + [ + 25512288.156, + 6684086.036899999715388 + ], + [ + 25512314.3239, + 6684076.544499999843538 + ], + [ + 25512364.3526, + 6684059.272599999792874 + ], + [ + 25512417.937199998646975, + 6684050.576899999752641 + ], + [ + 25512461.1138, + 6684037.759399999864399 + ], + [ + 25512503.8845, + 6684017.704699999652803 + ], + [ + 25512504.875599998980761, + 6684017.571899999864399 + ], + [ + 25512547.675299998372793, + 6683997.730700000189245 + ], + [ + 25512552.0966, + 6683999.045699999667704 + ], + [ + 25512555.52479999884963, + 6684004.52379999961704 + ], + [ + 25512555.048500001430511, + 6684005.667399999685585 + ], + [ + 25512555.7077, + 6684005.396099999547005 + ], + [ + 25512555.9936, + 6684001.8268 + ], + [ + 25512557.9199, + 6684003.3744 + ], + [ + 25512559.7823, + 6684004.870799999684095 + ], + [ + 25512557.840999998152256, + 6684006.8449 + ], + [ + 25512555.390500001609325, + 6684009.355700000189245 + ], + [ + 25512551.945099998265505, + 6684012.886099999770522 + ], + [ + 25512551.1206, + 6684014.789300000295043 + ], + [ + 25512533.2027, + 6684041.05489999987185 + ], + [ + 25512523.760200001299381, + 6684054.896599999628961 + ], + [ + 25512505.013, + 6684083.2248 + ], + [ + 25512491.8605, + 6684110.604799999855459 + ], + [ + 25512477.3605, + 6684168.102400000207126 + ], + [ + 25512451.83839999884367, + 6684117.75690000038594 + ], + [ + 25512449.954999998211861, + 6684120.426 + ], + [ + 25512449.070099998265505, + 6684120.891699999570847 + ], + [ + 25512445.8035, + 6684120.895600000396371 + ], + [ + 25512453.7144, + 6684145.737999999895692 + ], + [ + 25512451.837900001555681, + 6684151.470200000330806 + ], + [ + 25512445.322, + 6684152.975399999879301 + ], + [ + 25512441.2702, + 6684151.830900000408292 + ], + [ + 25512420.310600001364946, + 6684124.292899999767542 + ], + [ + 25512403.577799998223782, + 6684114.591099999845028 + ], + [ + 25512392.5887, + 6684112.39780000038445 + ], + [ + 25512375.671, + 6684115.822300000116229 + ], + [ + 25512282.8251, + 6684196.848500000312924 + ], + [ + 25512272.3997, + 6684215.00760000012815 + ] + ] + ] + } + } + ] +} diff --git a/services/management/commands/import_irregular_geometry.py b/services/management/commands/import_irregular_geometry.py new file mode 100644 index 000000000..43a24c696 --- /dev/null +++ b/services/management/commands/import_irregular_geometry.py @@ -0,0 +1,37 @@ +import json +import os + +from django.conf import settings +from django.contrib.gis.gdal import CoordTransform, SpatialReference +from django.contrib.gis.geos import GEOSGeometry, MultiPolygon +from django.core.management import BaseCommand + +from services.models import Unit +from services.models.unit import PROJECTION_SRID + + +def import_area(): + data_path = os.path.join(settings.BASE_DIR, "data") + file = os.path.join(data_path, "geometry/koirametsa.geojson") + with open(file, "r") as geojson_file: + geojson_data = json.load(geojson_file) + + geom_str = json.dumps(geojson_data["features"][0]["geometry"]) + geom = GEOSGeometry(geom_str) + multi_polygon = MultiPolygon([geom]) + + src_srs = SpatialReference(3879) + target_srs = SpatialReference(PROJECTION_SRID) + transform = CoordTransform(src_srs, target_srs) + multi_polygon.transform(transform) + + unit = Unit.objects.get(id=23795) # Östersundomin koirametsä + unit.geometry = multi_polygon + unit.save() + + +class Command(BaseCommand): + help = "Import area data for Östersundomin koirametsä (unit id 23795)." + + def handle(self, *args, **options): + import_area() diff --git a/services/tests/test_import_irregular_geometry.py b/services/tests/test_import_irregular_geometry.py new file mode 100644 index 000000000..8d152e50b --- /dev/null +++ b/services/tests/test_import_irregular_geometry.py @@ -0,0 +1,24 @@ +from datetime import datetime + +import pytest +import pytz +from django.core.management import call_command + +from services.models import Unit + + +@pytest.mark.django_db +def test_import_irregular_geometry(): + unit = Unit.objects.create( + id=23795, + name="Östersundomin koirametsä", + last_modified_time=datetime.now(pytz.utc), + ) + assert unit.geometry is None + + call_command("import_irregular_geometry") + unit.refresh_from_db() + + assert unit.geometry is not None + assert unit.geometry.geom_type == "MultiPolygon" + assert unit.geometry.area > 0