0

I have a dataset with a 2 columns: one column is of location names and the other column is polygons with the coordinates that outline the corresponding locations. This dataset is from a csv file that I've loaded into Databricks using Scala. Using this, I need to place another dataset containing lat/long points inside the polygons specified for each location (likely using the Magellan library), but both columns in the file are being read as strings when the second column needs to be casted as a polygon somehow for the magellan operations to work.

I've already tried casting as a polygon/shape, but nothing has worked so far. The dataset is formatted like this:

+-------------+--------------------+
|     location|             outline|
+-------------+--------------------+
|   Location A|POLYGON ((-111.11...|
|   Location B|POLYGON ((-99.111...|
|   Location C|POLYGON ((-99.111...|
|   Location D|POLYGON ((-99.111...|
|   Location E|POLYGON ((-99.111...|
|   Location F|POLYGON ((-111.11...|
|   Location G|POLYGON ((-99.111...|
|   Location H|POLYGON ((-99.111...|
|   Location I|POLYGON ((-111.11...|
|   Location J|POLYGON ((-111.11...|
|   Location K|POLYGON ((-99.111...|
|   Location L|POLYGON ((-99.111...|
|   Location M|POLYGON ((-99.111...|
|   Location N|POLYGON ((-99.111...|
|   Location O|POLYGON ((-111.11...|
|   Location P|POLYGON ((-99.111...|
|   Location Q|POLYGON ((-99.111...|
|   Location R|POLYGON ((-99.111...|
|   Location S|POLYGON ((-111.11...|
+-------------+--------------------+

so I just need the second column to be converted from a string to an actual polygon.

I need the second column (outline) to be an actual polygon (not a string) so that I can place points inside of the polygons.

user261011
  • 21
  • 2
  • You can find the answer here: https://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon – Pablo López Gallego Apr 15 '19 at 15:33
  • @PabloLópezGallego I think you misunderstood where I was having trouble. I know how to place the points inside a polygon but, in order to do that, I need the outline column to be read/ converted to an actual polygon whereas right now, it is being read as a string – user261011 Apr 15 '19 at 15:43
  • unless the library you're using provides a built-in string to polygon parser, you probably have to write your own. – sachav Apr 15 '19 at 15:52

1 Answers1

1

Are you already try the library magellan.Polygon?, this is an example to convert Polygon to DF:

import magellan.Polygon

case class PolygonExample(polygon: Polygon)

val ring = Array(Point(1.0, 1.0), Point(1.0, -1.0),
      Point(-1.0, -1.0), Point(-1.0, 1.0), Point(1.0, 1.0))
val polygon = Polygon(Array(0), ring)

val polygons = sc.parallelize(Seq(
      PolygonExample(Polygon(Array(0), ring))
    )).toDF()

Result:

polygons.show()
+--------------------+
|             polygon|
+--------------------+
|magellan.Polygon@...|
+--------------------+