Skip to content

Commit 150fe63

Browse files
authored
Merge pull request #7 from abduhbm/pixel-is-point
Add support for pixel is point convention
2 parents bdb752f + 3fa88b9 commit 150fe63

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ def zmap_object(tmpdir):
2626
x.write(z_text)
2727
z_obj = ZMAPGrid(x.strpath)
2828
yield z_obj
29+
30+
31+
@pytest.fixture
32+
def zmap_object_pixel_is_point(tmpdir):
33+
x = tmpdir.join("test.dat")
34+
x.write(z_text)
35+
z_obj = ZMAPGrid(x.strpath, pixel_is_point=True)
36+
yield z_obj

tests/test_export.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def test_export_to_csv(zmap_object, tmpdir):
1313
assert lines[1] == "0.0,300.0,nan\n"
1414

1515

16+
def test_export_to_csv_pixel_is_point(zmap_object_pixel_is_point, tmpdir):
17+
x = tmpdir.join("output.csv")
18+
zmap_object_pixel_is_point.to_csv(x.strpath)
19+
lines = x.readlines()
20+
assert len(lines) == 25
21+
assert lines[0] == "# X,Y,Z\n"
22+
assert lines[1] == "25.0,275.0,nan\n"
23+
24+
1625
def test_export_to_geojson(zmap_object, tmpdir):
1726
x = tmpdir.join("output.json")
1827
zmap_object.to_geojson(x.strpath)
@@ -23,6 +32,16 @@ def test_export_to_geojson(zmap_object, tmpdir):
2332
assert [0.0, 60.0, 88.0] in d.get("coordinates")
2433

2534

35+
def test_export_to_geojson_pixel_is_point(zmap_object_pixel_is_point, tmpdir):
36+
x = tmpdir.join("output.json")
37+
zmap_object_pixel_is_point.to_geojson(x.strpath)
38+
d = json.load(x)
39+
assert sorted(list(d.keys())) == ["coordinates", "type"]
40+
assert d.get("type") == "MultiPoint"
41+
assert len(d.get("coordinates")) == 24
42+
assert [25.0, 175.0, 3.0] in d.get("coordinates")
43+
44+
2645
def test_export_to_wkt(zmap_object, tmpdir):
2746
x = tmpdir.join("output.wkt")
2847
zmap_object.to_wkt(x.strpath)
@@ -31,6 +50,14 @@ def test_export_to_wkt(zmap_object, tmpdir):
3150
assert line.startswith("MULTIPOINT ((0.0000000 300.0000000 nan),")
3251

3352

53+
def test_export_to_wkt_pixel_is_point(zmap_object_pixel_is_point, tmpdir):
54+
x = tmpdir.join("output.wkt")
55+
zmap_object_pixel_is_point.to_wkt(x.strpath)
56+
with open(x.strpath) as f:
57+
line = f.readline()
58+
assert line.startswith("MULTIPOINT ((25.0000000 275.0000000 nan),")
59+
60+
3461
def test_export_to_wkt_with_precision(zmap_object, tmpdir):
3562
x = tmpdir.join("output.wkt")
3663
zmap_object.to_wkt(x.strpath, precision=2)

zmapio/zmap.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(
2323
min_y=None,
2424
max_y=None,
2525
z_values=None,
26+
pixel_is_point=False,
2627
**kwargs
2728
):
2829
self.comments = comments
@@ -39,6 +40,7 @@ def __init__(
3940
self.max_x = max_x
4041
self.min_y = min_y
4142
self.max_y = max_y
43+
self.pixel_is_point = pixel_is_point
4244

4345
if file_ref:
4446
x, y, z = self.read(file_ref, **kwargs)
@@ -80,8 +82,17 @@ def read(self, file_ref, dtype=np.float64):
8082

8183
z[z == self.null_value] = np.nan
8284
z = z.reshape((self.no_cols, self.no_rows))
83-
x = np.linspace(self.min_x, self.max_x, self.no_cols)
84-
y = np.linspace(self.max_y, self.min_y, self.no_rows)
85+
if self.pixel_is_point:
86+
# get cell size
87+
dx = (self.max_x - self.min_x) / self.no_cols
88+
dy = (self.max_y - self.min_y) / self.no_rows
89+
x = np.linspace(self.min_x + (dx / 2), self.max_x - (dx / 2), self.no_cols)
90+
y = np.linspace(self.max_y - (dy / 2), self.min_y + (dy / 2), self.no_rows)
91+
92+
else:
93+
x = np.linspace(self.min_x, self.max_x, self.no_cols)
94+
y = np.linspace(self.max_y, self.min_y, self.no_rows)
95+
8596
x, y = np.meshgrid(x, y)
8697

8798
return x, y, z

0 commit comments

Comments
 (0)