{"id":261,"date":"2021-06-21T15:14:50","date_gmt":"2021-06-21T15:14:50","guid":{"rendered":"https:\/\/ml-gis-service.com\/?p=261"},"modified":"2021-06-28T10:29:12","modified_gmt":"2021-06-28T10:29:12","slug":"toolbox-dataframe-points-to-geoseries","status":"publish","type":"post","link":"https:\/\/ml-gis-service.com\/index.php\/2021\/06\/21\/toolbox-dataframe-points-to-geoseries\/","title":{"rendered":"Toolbox: DataFrame points to GeoSeries"},"content":{"rendered":"\n<p>Tutorial presents how to transform points as floats written in <code>DataFrame<\/code> into <code>GeoSeries<\/code> object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updates<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>2021-06-28: GeoPandas method <code>geopandas.points_from_xy<\/code> included in the tutorial due to the nice response from GeoPandas developers on Twitter (<a href=\"https:\/\/twitter.com\/geopandas\/status\/1409439285162479617\">here<\/a>).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Requirements<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><code>pandas<\/code> to read <code>csv<\/code> files with float coordinates,<\/li><li><code>geopandas<\/code> to create <code>GeoSeries<\/code>.<\/li><\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import geopandas as gpd\nimport pandas as pd<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Proof of Concept<\/h2>\n\n\n\n<p>GeoPandas allows us to transform any list of points into Point() with <code>geopandas.points_from_xy()<\/code> method:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Transformation of floats to the Point\n\nlat = 51.5074\nlon = 0.1278\n\npoint = gpd.points_from_xy([lat], [lon])<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Function<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Transform sample\n\ndef lat_lon_to_point(dataframe, lon_col='longitude', lat_col='latitude', crs='WGS84'):\n    \"\"\"Function transform longitude and latitude coordinates into GeoSeries.\n    \n    INPUT:\n    \n    :param dataframe: DataFrame to be transformed,\n    :param lon_col: (str) longitude column name, default is 'longitude',\n    :param lat_col: (str) latitude column name, default is 'latitude',\n    :param crs: (str) crs of the output geoseries.\n    \n    OUTPUT:\n    \n    :return: (GeoPandas GeoSeries object)\n    \"\"\"\n    geometry = gpd.points_from_xy(dataframe[lon_col], dataframe[lat_col])\n    geoseries = gpd.GeoSeries(geometry)\n    geoseries.name = 'geometry'\n    geoseries.crs = crs\n    \n    return geoseries<\/pre>\n\n\n\n<p>The important thing is that longitude is <code>x<\/code> coordinate and latitude is <code>y<\/code> coordinate. We must set coordinate reference system (CRS) to create valid <code>GeoSeries<\/code>. In most cases CRS of point measurements is <a href=\"https:\/\/en.wikipedia.org\/wiki\/World_Geodetic_System\">WGS84<\/a> popularized by the Global Positioning Systems.<\/p>\n\n\n\n<p>You may now work with your <code>GeoSeries<\/code> and use spatial methods and attributes of this object!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Requirements &#8211; old algorithm<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><code>pandas<\/code> to read <code>csv<\/code> files with float coordinates,<\/li><li><code>geopandas<\/code> to create <code>GeoSeries<\/code>,<\/li><li><code>shapely<\/code> to transform floats into <code>Point<\/code>.<\/li><\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import geopandas as gpd\nimport pandas as pd\nfrom shapely.geometry import Point<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Proof of Concept &#8211; old algorithm<\/h2>\n\n\n\n<p>Two floats may be easily transformed into <code>Point<\/code> with <code>shapely<\/code>. List or tuple of two floats is passed into the <code>Point<\/code> class constructor:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Transformation of floats to the Point\n\nlat = 51.5074\nlon = 0.1278\n\nsample_coordinate = [lon, lat]  # x, y\npoint = Point(sample_coordinate)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Function &#8211; old algorithm<\/h2>\n\n\n\n<p>The hardest part of transformation is to transform <code>DataFrame<\/code> float columns at once. We use for it <code>.apply()<\/code> method and <code>lambda<\/code> function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Transform float columns into geoseries\n\ndef lat_lon_to_point(dataframe, lon_col='longitude', lat_col='latitude', crs='WGS84'):\n    \"\"\"Function transform longitude and latitude coordinates into GeoSeries.\n    \n    INPUT:\n    \n    :param dataframe: DataFrame to be transformed,\n    :param lon_col: (str) longitude column name, default is 'longitude',\n    :param lat_col: (str) latitude column name, default is 'latitude',\n    :param crs: (str) crs of the output geoseries.\n    \n    OUTPUT:\n    \n    :return: (GeoPandas GeoSeries object)\n    \"\"\"\n    geometry = dataframe.apply(lambda x: Point([x[lon_col], x[lat_col]]), axis=1)\n    geoseries = gpd.GeoSeries(geometry)\n    geoseries.name = 'geometry'\n    geoseries.crs = crs\n    \n    return geoseries<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python function to transform DataFrame with float coordinates to GeoSeries (GeoDataFrame).<\/p>\n","protected":false},"author":1,"featured_media":263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,17],"tags":[47,59,60,57,56,61,64,58,7,63,62,34,65],"class_list":["post-261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-engineering","category-python","category-scripts","tag-data-science","tag-float","tag-float-to-point","tag-geopandas","tag-geoseries","tag-geospatial","tag-pandas","tag-points","tag-python","tag-shapely","tag-spatial","tag-spatial-statistics","tag-transformation"],"_links":{"self":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":2,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/261\/revisions\/269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media\/263"}],"wp:attachment":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}