diff --git a/pvlib/iotools/psm4.py b/pvlib/iotools/psm4.py
index 808899362c..e9e7ef9f0d 100644
--- a/pvlib/iotools/psm4.py
+++ b/pvlib/iotools/psm4.py
@@ -174,12 +174,6 @@ def get_nsrdb_psm4_aggregated(latitude, longitude, api_key, email,
.. [2] `NSRDB GOES Aggregated V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -192,7 +186,7 @@ def get_nsrdb_psm4_aggregated(latitude, longitude, api_key, email,
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
@@ -314,12 +308,6 @@ def get_nsrdb_psm4_tmy(latitude, longitude, api_key, email, year='tmy',
.. [2] `NSRDB GOES Tmy V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -332,7 +320,7 @@ def get_nsrdb_psm4_tmy(latitude, longitude, api_key, email, year='tmy',
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
@@ -450,12 +438,6 @@ def get_nsrdb_psm4_conus(latitude, longitude, api_key, email, year,
.. [2] `NSRDB GOES Conus V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -468,7 +450,7 @@ def get_nsrdb_psm4_conus(latitude, longitude, api_key, email, year,
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
@@ -589,12 +571,6 @@ def get_nsrdb_psm4_full_disc(latitude, longitude, api_key, email,
.. [2] `NSRDB GOES Full Disc V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -607,7 +583,7 @@ def get_nsrdb_psm4_full_disc(latitude, longitude, api_key, email,
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
@@ -738,12 +714,6 @@ def get_nsrdb_psm4_polar(latitude, longitude, api_key, email,
.. [3] `NSRDB Polar V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -756,7 +726,7 @@ def get_nsrdb_psm4_polar(latitude, longitude, api_key, email,
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
@@ -887,12 +857,6 @@ def get_nsrdb_psm4_polar_tmy(latitude, longitude, api_key, email, year='tmy',
.. [3] `NSRDB Polar Tmy V4.0.0
`_
"""
- # The well know text (WKT) representation of geometry notation is strict.
- # A POINT object is a string with longitude first, then the latitude, with
- # four decimals each, and exactly one space between them.
- longitude = ('%9.4f' % longitude).strip()
- latitude = ('%8.4f' % latitude).strip()
- # TODO: make format_WKT(object_type, *args) in tools.py
# convert pvlib names in parameters to PSM4 convention
parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters]
@@ -905,7 +869,7 @@ def get_nsrdb_psm4_polar_tmy(latitude, longitude, api_key, email, year='tmy',
'affiliation': affiliation,
'reason': PVLIB_PYTHON,
'mailing_list': 'false',
- 'wkt': 'POINT(%s %s)' % (longitude, latitude),
+ 'wkt': tools._format_wkt_point(latitude, longitude),
'names': year,
'attributes': ','.join(parameters),
'leap_day': str(leap_day).lower(),
diff --git a/pvlib/tools.py b/pvlib/tools.py
index a81306b60c..67aaa6dd09 100644
--- a/pvlib/tools.py
+++ b/pvlib/tools.py
@@ -630,3 +630,27 @@ def _file_context_manager(filename_or_object, mode='r', encoding=None):
# otherwise, assume a filename or path
context = open(str(filename_or_object), mode=mode, encoding=encoding)
return context
+
+
+def _format_wkt_point(latitude, longitude):
+ """
+ Format a location as a well-known text (WKT) POINT string.
+
+ The WKT representation of geometry is strict: a POINT object lists
+ longitude first, then latitude, each rounded to four decimal places,
+ with exactly one space between them. This format is required by
+ the NSRDB PSM4 API, see :py:mod:`pvlib.iotools.psm4`.
+
+ Parameters
+ ----------
+ latitude : float or int
+ Latitude in decimal degrees, between -90 and 90, north is positive.
+ longitude : float or int
+ Longitude in decimal degrees, between -180 and 180, east is positive.
+
+ Returns
+ -------
+ str
+ The location as a WKT POINT, e.g. ``'POINT(-105.1800 39.7400)'``.
+ """
+ return f'POINT({longitude:.4f} {latitude:.4f})' # noqa: E231
diff --git a/tests/test_tools.py b/tests/test_tools.py
index 4b733ad711..8a45226c21 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -283,3 +283,27 @@ def test__file_context_manager():
buffer = StringIO("test content")
with tools._file_context_manager(buffer) as obj:
assert obj.read() == "test content"
+
+
+@pytest.mark.parametrize('latitude, longitude, expected', [
+ (39.742, -105.18, 'POINT(-105.1800 39.7420)'),
+ (40, -105, 'POINT(-105.0000 40.0000)'),
+ (-33.86786, 151.20732, 'POINT(151.2073 -33.8679)'),
+ (0, 0, 'POINT(0.0000 0.0000)'),
+ (89.99999, 179.99999, 'POINT(180.0000 90.0000)'),
+ (np.float64(12.3456789), np.float64(-1.23456789),
+ 'POINT(-1.2346 12.3457)'),
+])
+def test__format_wkt_point(latitude, longitude, expected):
+ assert tools._format_wkt_point(latitude, longitude) == expected
+
+
+@pytest.mark.parametrize('latitude, longitude', [
+ (39.742, -105.18), (-33.86785, 151.20732), (0, 0), (-90, 180),
+])
+def test__format_wkt_point_matches_legacy_format(latitude, longitude):
+ # the helper replaces the formatting previously inlined in
+ # pvlib.iotools.psm4; it must give the same string
+ legacy = 'POINT(%s %s)' % (('%9.4f' % longitude).strip(),
+ ('%8.4f' % latitude).strip())
+ assert tools._format_wkt_point(latitude, longitude) == legacy