faninsar.query.Points#

class faninsar.query.Points(points: Sequence[float | Sequence[float]], crs: CrsLike = None, dtype: np.dtype = <class 'numpy.float32'>)[source]#

Bases: object

A class to represent a collection of points.

Examples

>>> pts = Points([[1, 2], [2, 3], [3, 4]])
>>> pt = pts[1]

set difference of two Points:

>>> pts - pt
Points:
    x    y
0  1.0  2.0
1  3.0  4.0
[count=2, crs='None']

set union of two Points:

>>> pts + Points([1, 5])
Points:
    x    y
0  1.0  2.0
1  2.0  3.0
2  3.0  4.0
3  1.0  5.0
[count=4, crs='None']

in operator:

>>> pts[1] in pts
True
>>> Points([1, 5]) in pts
False

convert to numpy array using np.array:

>>> np.array(pts, dtype=np.int16)
array([[1, 2],
        [2, 3],
        [3, 4]], dtype=int16)

extract x and y coordinates:

>>> pts.x
array([1., 2., 3.], dtype=float32)
>>> pts.y
array([2., 3., 4.], dtype=float32)

extract values:

>>> pts.values
array([[1., 2.],
        [2., 3.],
        [3., 4.]], dtype=float32)

convert to GeoDataFrame:

>>> pts.to_GeoDataFrame()
    x       y       geometry
0   1.0     2.0     POINT (1.00000 2.00000)
1   2.0     3.0     POINT (2.00000 3.00000)
2   3.0     4.0     POINT (3.00000 4.00000)
__init__(points: Sequence[float | Sequence[float]], crs: CrsLike = None, dtype: np.dtype = <class 'numpy.float32'>) None[source]#

Initialize a Points object.

Parameters:
  • points (Sequence[float | Sequence[float]]) – The points to be sampled. The shape of the points can be (2) or (n, 2) where n is the number of points. The first column is the x coordinate and the second column is the y coordinate. if the shape is (2), the points will be reshaped to (1, 2).

  • crs (Any, optional, default: None) – The coordinate reference system of the points. Can be any object that can be passed to pyproj.crs.CRS.from_user_input().

  • dtype (np.dtype, optional) – The data type of the points. Default is np.float32.

Raises:

ValueError – If the shape of the points is not (n, 2).

Methods

__init__(points[, crs, dtype])

Initialize a Points object.

from_csv(filename[, x_field, y_field, crs])

Initialize a Points object from a csv/txt file.

from_geo_dataframe(gdf[, x_field, y_field])

Initialize a Points object from a GeoDataFrame.

from_shapefile(filename, **kwargs)

Initialize a Points object from a file.

set_crs(crs)

Set the coordinate reference system of the points.

to_DataFrame()

Convert the Points to a DataFrame.

to_GeoDataFrame()

Convert the Points to a GeoDataFrame.

to_crs(crs)

Convert the points values to a new coordinate reference system.

to_shapefile(filename, **kwargs)

Save the Points to a shapefile.

Attributes

crs

Return the coordinate reference system of the points.

dtype

Return the data type of the points.

values

Return the values of the points with shape (n, 2).

x

Return the x coordinates of the points.

y

Return the y coordinates of the points.

classmethod from_csv(filename: str | Path, x_field: str = 'auto', y_field: str = 'auto', crs: CrsLike = None, **kwargs) Points[source]#

Initialize a Points object from a csv/txt file.

Parameters:
  • filename (str | Path) – The path to the csv/txt file.

  • x_field (str, optional, default: "auto") –

    The field name of the x/y coordinates. If “auto”, will try to find the field name automatically from following fields (case insensitive):

    • x : x, xs, lon, longitude

    • y : y, ys, lat, latitude

  • y_field (str, optional, default: "auto") –

    The field name of the x/y coordinates. If “auto”, will try to find the field name automatically from following fields (case insensitive):

    • x : x, xs, lon, longitude

    • y : y, ys, lat, latitude

  • crs (Any, optional, default: None) – The coordinate reference system of the points. Can be any object that can be passed to pyproj.crs.CRS.from_user_input().

  • **kwargs (dict) – Other parameters passed to pandas.read_csv().

Returns:

The Points object.

Return type:

Points

classmethod from_geo_dataframe(gdf: GeoDataFrame, x_field: str = 'auto', y_field: str = 'auto') Points[source]#

Initialize a Points object from a GeoDataFrame.

Parameters:
  • gdf (gpd.GeoDataFrame) – The GeoDataFrame to be parsed.

  • x_field (str, optional, default: "auto") –

    The field name of the x/y coordinates if geometry not exists. If auto, will try to find the field name automatically from following fields (case insensitive):

    • x: x, xs, lon, longitude, long, longs, longitudes

    • y: y, ys, lat, latitude, lats, latitudes

  • y_field (str, optional, default: "auto") –

    The field name of the x/y coordinates if geometry not exists. If auto, will try to find the field name automatically from following fields (case insensitive):

    • x: x, xs, lon, longitude, long, longs, longitudes

    • y: y, ys, lat, latitude, lats, latitudes

Returns:

The Points object.

Return type:

Points

classmethod from_shapefile(filename: str | Path, **kwargs) Points[source]#

Initialize a Points object from a file.

Parameters:
Returns:

The Points object.

Return type:

Points

set_crs(crs: CrsLike) None[source]#

Set the coordinate reference system of the points.

Warning

This method will only set the crs attribute without converting the points to a new coordinate reference system. If you want to convert the points values to a new coordinate, please use to_crs()

to_DataFrame() DataFrame[source]#

Convert the Points to a DataFrame.

Returns:

The DataFrame with columns x and y.

Return type:

pd.DataFrame

to_GeoDataFrame() GeoDataFrame[source]#

Convert the Points to a GeoDataFrame.

Returns:

The GeoDataFrame.

Return type:

gpd.GeoDataFrame

to_crs(crs: CrsLike) Points[source]#

Convert the points values to a new coordinate reference system.

Parameters:

crs (Any) – The new coordinate reference system. Can be any object that can be passed to pyproj.crs.CRS.from_user_input().

Returns:

The points in the new coordinate reference system.

Return type:

Points

to_shapefile(filename: str | Path, **kwargs) None[source]#

Save the Points to a shapefile.

Parameters:
property crs: CRS | None#

Return the coordinate reference system of the points.

property dtype: dtype#

Return the data type of the points.

property values: ndarray#

Return the values of the points with shape (n, 2).

n is the number of points.

property x: ndarray#

Return the x coordinates of the points.

property y: ndarray#

Return the y coordinates of the points.