Skip to content

Commit

Permalink
Feature/coordinates fixes (#43)
Browse files Browse the repository at this point in the history
* small fixes

* black and isort

* fix long line

* fix circular import

* fix

* fix

* fix typos

Co-authored-by: ahartikainen <[email protected]>
  • Loading branch information
ahartikainen and ahartikainen authored Jun 13, 2020
1 parent 7cc7f16 commit d706283
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 79 deletions.
3 changes: 2 additions & 1 deletion pyinfraformat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# pylint: disable=wildcard-import
"""pyinfraformat, Python library for Finnish infraformat files."""
import logging
from .plots import *

from .core import *
from .exceptions import *
from .plots import *

logger = logging.getLogger("pyinfraformat")

Expand Down
2 changes: 1 addition & 1 deletion pyinfraformat/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=wildcard-import
"""Core pyinfraformat functionality."""
from .coord_utils import *
from .core import *
from .io import *
from .utils import *
from .coord_utils import *
55 changes: 30 additions & 25 deletions pyinfraformat/core/coord_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import logging
import re
from copy import deepcopy
from pyproj import Transformer
from matplotlib.tri import Triangulation, LinearTriInterpolator

import numpy as np
import pandas as pd
from .core import Holes, Hole
from matplotlib.tri import LinearTriInterpolator, Triangulation
from pyproj import Transformer

from .core import Hole, Holes

__all__ = ["project_holes"]

Expand Down Expand Up @@ -47,7 +49,7 @@

def coord_string_fix(input_string):
"""Try to fix coordinate systems string into machine readable."""
abbreviations = {"HKI": "HELSINKI", "YKJ": "KKJ3"}
abbreviations = {"HKI": "HELSINKI", "YKJ": "KKJ3", "KKJ": "KKJ3"}
input_string = input_string.upper()
input_string = abbreviations.get(input_string, input_string)
common_separators = r"[,. :\_-]"
Expand All @@ -56,8 +58,8 @@ def coord_string_fix(input_string):
return "-".join(re.split(common_separators, input_string))


def change_x_to_y(holes):
"""Change holes x to y and y to x. Creates a deepcopy."""
def flip_xy(holes):
"""Change holes x,y to y,x."""
if isinstance(holes, Holes):
holes_copy = deepcopy(holes)
for hole in holes_copy:
Expand Down Expand Up @@ -165,7 +167,7 @@ def project_points(x, y, input_system="EPSG:3067", output_system="EPSG:4326"):
input_epsg = EPSG_SYSTEMS[name]
else:
raise ValueError(
"Ivalid input_system parameter {}, possible systems: {}".format(
"Invalid input_system parameter {}, possible systems: {}".format(
name, list(EPSG_SYSTEMS.keys())
)
)
Expand All @@ -178,7 +180,7 @@ def project_points(x, y, input_system="EPSG:3067", output_system="EPSG:4326"):
output_epsg = EPSG_SYSTEMS[name]
else:
raise ValueError(
"Ivalid output_system parameter {}, possible systems: {}".format(
"Invalid output_system parameter {}, possible systems: {}".format(
name, list(EPSG_SYSTEMS.keys())
)
)
Expand All @@ -195,7 +197,7 @@ def project_points(x, y, input_system="EPSG:3067", output_system="EPSG:4326"):
return x, y


def check_hole(hole, bbox):
def check_hole_inside_bbox(hole, bbox):
"""Check if hole point is inside bbox.
Returns boolean.
Expand All @@ -206,7 +208,7 @@ def check_hole(hole, bbox):
return False


def check_area(holes, country="FI"):
def check_hole_in_country(holes, country="FI"):
"""Check if holes or hole points are inside country bbox. Has to be in some system with EPSG.
Returns boolean.
Expand All @@ -220,7 +222,7 @@ def check_area(holes, country="FI"):
elif isinstance(holes, Hole):
input_str = holes.fileheader.KJ["Coordinate system"]
else:
raise ValueError("holes -parameter is unkown input type")
raise ValueError("holes -parameter is unknown input type")
input_str = coord_string_fix(input_str)

if input_str in EPSG_SYSTEMS:
Expand All @@ -240,9 +242,9 @@ def check_area(holes, country="FI"):
else:
raise ValueError("Input has to be in known epsg system.", input_str)
if isinstance(holes, Holes):
return all(check_hole(hole, bbox) for hole in holes)
return all(check_hole_inside_bbox(hole, bbox) for hole in holes)
else:
return check_hole(holes, bbox)
return check_hole_inside_bbox(holes, bbox)


def get_n43_n60_points():
Expand Down Expand Up @@ -325,11 +327,11 @@ def height_systems_diff(points, input_system, output_system):
ndarray
list of height differences (mm). Nan for points outside triangulation.
"""
points = np.array(points)
points = np.asarray(points)
if len(points.shape) == 1 and len(points) == 2:
points = points[:, None].T
points = points[None, :]
if points.shape[1] != 2:
raise ValueError("Points has to be 2D -array with (n, 2) shape")
raise ValueError("Points has to be 2D -array with shape=(n, 2)")
input_system = input_system.upper()
output_system = output_system.upper()
if input_system == output_system:
Expand Down Expand Up @@ -365,7 +367,10 @@ def height_systems_diff(points, input_system, output_system):
diff = -INTERPOLATORS[key](*points.T).data
else:
raise ValueError(
"input_system or output_system invalid. Possible values are N43, N60, N2000."
(
"input_system ({}) or output_system ({}) invalid."
" Possible values are N43, N60, N2000."
).format(input_system, output_system)
)
return diff

Expand All @@ -387,14 +392,14 @@ def project_hole(hole, output_epsg="EPSG:4326", output_height=False):
hole : Hole -object
Copy of hole with coordinates transformed
"""
epsg_names = {EPSG_SYSTEMS[i]: i for i in EPSG_SYSTEMS}
epsg_names = {key: value for value, key in EPSG_SYSTEMS.items()}
other_systems = {"HELSINKI": proj_helsinki, "ESPOO": proj_espoo, "PORVOO": proj_porvoo}

hole_copy = deepcopy(hole)
if not isinstance(hole, Hole):
raise ValueError("hole -parameter invalid")
if output_epsg not in epsg_names:
raise ValueError("Unkown or not implemented EPSG as output_epsg")
raise ValueError("Unknown or not implemented EPSG as output_epsg")
if (
hasattr(hole_copy, "fileheader")
and hasattr(hole_copy.fileheader, "KJ")
Expand Down Expand Up @@ -424,7 +429,7 @@ def project_hole(hole, output_epsg="EPSG:4326", output_height=False):
func = other_systems[input_str]
x, y, input_epsg = func(x, y)
else:
msg = "Unkown or not implemented EPSG in holes {}".format(input_str)
msg = "Unknown or not implemented EPSG in holes {}".format(input_str)
raise ValueError(msg)

key = (input_epsg, output_epsg)
Expand Down Expand Up @@ -453,7 +458,7 @@ def project_hole(hole, output_epsg="EPSG:4326", output_height=False):
except KeyError:
raise ValueError("Hole has no height system")
if input_system not in ["N43", "N60", "N2000"]:
raise ValueError("Hole has no unknown heigth system:", input_system)
raise ValueError("Hole has unknown heigth system:", input_system)

diff = height_systems_diff(point, input_system, output_height)
hole_copy.header.XY["Z-start"] += round(float(diff), 3)
Expand All @@ -464,7 +469,7 @@ def project_hole(hole, output_epsg="EPSG:4326", output_height=False):
def project_holes(holes, output="EPSG:4326", check="Finland", output_height=False):
"""Transform holes -objects coordinates.
Transform holes coordinates, drops invalid holes. Warns into logger.
Transform holes coordinates, drops invalid holes.
Parameters
----------
Expand Down Expand Up @@ -525,14 +530,14 @@ def project_holes(holes, output="EPSG:4326", check="Finland", output_height=Fals
hole = deepcopy(holes)
return_value = project_hole(hole, output_epsg, output_height)
else:
raise ValueError("holes -parameter is unkown input type")
raise ValueError("holes -parameter is unknown input type")

if check and check.upper() == "FINLAND":
if not check_area(return_value, "FI"):
if not check_hole_in_country(return_value, "FI"):
msg = "Holes are not inside Finland"
logger.critical(msg)
if check and check.upper() == "ESTONIA":
if not check_area(return_value, "EE"):
if not check_hole_in_country(return_value, "EE"):
msg = "Holes are not inside Estonia"
logger.critical(msg)
return return_value
10 changes: 6 additions & 4 deletions pyinfraformat/core/core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Core function for pyinfraformat."""
from datetime import datetime
from gc import collect
import logging
import os
from datetime import datetime
from gc import collect

import pandas as pd

from ..plots.holes import plot_hole
from ..exceptions import FileExtensionMissingError

logger = logging.getLogger("pyinfraformat")
Expand Down Expand Up @@ -448,10 +448,12 @@ def plot(self, backend="matplotlib"):
-------
figure : matplotlib figure or mpld3 html
"""
from ..plots.holes import plot_hole

return plot_hole(self, backend)

def _repr_html_(self):
return plot_hole(self, backend="mpld3")
return self.plot(backend="mpld3")


class FileHeader:
Expand Down
10 changes: 6 additions & 4 deletions pyinfraformat/core/io.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# pylint: disable=try-except-raise
"""Input and output methods."""
from collections import Counter
from glob import glob
import logging
import os
from collections import Counter
from glob import glob

import xmltodict
from owslib.wfs import WebFeatureService
from .core import Holes, Hole

from ..exceptions import PathNotFoundError
from .utils import identifiers, is_number
from .coord_utils import EPSG_SYSTEMS, project_points
from .core import Hole, Holes
from .utils import identifiers, is_number

logger = logging.getLogger("pyinfraformat")

Expand Down
1 change: 1 addition & 0 deletions pyinfraformat/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""General information concerning Finnish Infraformat."""
import logging

import numpy as np

__all__ = ["identifiers", "print_info"]
Expand Down
8 changes: 5 additions & 3 deletions pyinfraformat/plots/holes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Plot diagrams for a single hole."""
import gc
import json
import mpld3
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import mpld3

__all__ = ["plot_hole"]

Expand Down
10 changes: 6 additions & 4 deletions pyinfraformat/plots/maps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Plot a html folium map from holes object."""
from itertools import cycle
import folium

import numpy as np

import branca
import folium
from folium.plugins import MarkerCluster, MeasureControl, MousePosition
import numpy as np

from .holes import plot_hole
from ..core import Holes
from ..core.coord_utils import project_points, EPSG_SYSTEMS, coord_string_fix
from ..core.coord_utils import EPSG_SYSTEMS, coord_string_fix, project_points
from .holes import plot_hole

__all__ = ["plot_map"]

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re

from setuptools import setup, find_packages
from setuptools import find_packages, setup

PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
REQUIREMENTS_FILE = os.path.join(PROJECT_ROOT, "requirements.txt")
Expand Down
Loading

0 comments on commit d706283

Please sign in to comment.