Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create an Polygon Object from list of CLLocationCoordinate2D #67

Closed
SebastianBoldt opened this issue Nov 30, 2017 · 6 comments
Closed

Comments

@SebastianBoldt
Copy link

SebastianBoldt commented Nov 30, 2017

Hey Guys,

I am currently trying to create Geometry Objects from a list a CLLocationCoordinate2D-Objects.
This is my approach:

import Foundation
import MapKit
import GEOSwift

class MyPolygon {
	var coordinates: [CLLocationCoordinate2D] = []
	init(coordinates: [CLLocationCoordinate2D]) {
		self.coordinates = coordinates
	}
	
	var geoPolygon: Polygon {
		let pairs: [String] = coordinates.flatMap { String("\($0.latitude)) \($0.longitude )")}
		let joined = pairs.joined(separator: ", ")
		return Polygon(WKT: "POLYGON((\(joined)))")!
	}
}

The joined String could look like this:

"POLYGON((51.0693791021764 9.75201444533336, 51.1328987236114 9.67614867733331, 51.2121755537486 9.58763861466671, 51.2517628689227 9.56235002533339, 51.2913161313519 9.53706143599998, 51.3466335100652 9.52441714133342, 51.4334260815772 9.51177284666666, 51.5200540729691 9.48648425733335, 51.6457648636909 9.46119566800003, 51.7554766499544 9.42326278399996, 51.8414922247152 9.39797419466664, 51.9585218712106 9.39797419466664, 52.0052483952469 9.39797419466664, 52.0519261862681 9.4106184893334, 52.1063220425348 9.44855137333327, 52.1373756320464 9.499128552, 52.1528943136927 9.52441714133342, 52.168407587594 9.57499432000005, 52.1761621968738 9.61292720400002, 52.1916673605595 9.67614867733331, 52.2304066207189 9.8278802133333, 52.2768491510004 9.99225604400005, 52.3000521840339 10.1692761693334, 52.3232430657399 10.3210077053333, 52.3232430657399 10.4600949466667, 52.3232430657399 10.599182188, 52.3232430657399 10.6876922506667, 52.3232430657399 10.8141351973333, 52.3155141218271 10.864712376, 52.2923191899635 10.940578144, 52.2613737103096 10.9911553226667, 52.2381504193787 11.016443912, 52.2226614710605 11.0417325013333, 52.1761621968738 11.09230968, 52.1373756320464 11.1175982693334, 52.0985552641519 11.1555311533334, 52.036372336149 11.206108332, 51.9585218712106 11.2440412160001, 51.9039459736845 11.2566855106666, 51.8571137937767 11.2819741, 51.7711280511801 11.2946183946667, 51.6692969280776 11.2946183946667, 51.5750953384108 11.2946183946667, 51.512185598856 11.2946183946667, 51.4649462056936 11.2946183946667, 51.4097717070348 11.2693298053334, 51.3782134946566 11.2440412160001, 51.3308353514949 11.2187526266667, 51.2596762452943 11.1555311533334, 51.1963310905783 11.1049539746667, 51.1328987236114 11.054376796, 51.0932091880119 11.0037996173334, 51.0693791021764 10.978511028, 51.053485558395 10.9532224386667, 51.0375865579418 10.9152895546667, 51.0216821001682 10.8773566706667, 51.0137278245838 10.8267794920001, 51.0137278245838 10.8014909026667, 51.0137278245838 10.750913724, 50.9978151796162 10.6876922506667, 50.9898568100717 10.6371150720001, 50.9739359764586 10.5865378933334, 50.9500444885243 10.4600949466667, 50.9420779288883 10.4221620626667, 50.9181700578872 10.3589405893333, 50.91019803659 10.3210077053333, 50.9022246496782 10.2324976426666, 50.9022246496782 10.1692761693334, 50.9022246496782 10.1313432853334, 50.9022246496782 10.068121812, 50.9022246496782 10.0175446333334, 50.9022246496782 9.97961174933329, 50.9181700578872 9.916390276, 50.9261407136496 9.87845739200003, 50.9420779288883 9.84052450800006, 50.9420779288883 9.81523591866664, 50.9500444885243 9.80259162399999, 50.9500444885243 9.78994732933333, 50.9580096829447 9.78994732933333, 50.9580096829447 9.77730303466667, 50.9659735122294 9.77730303466667, 50.9739359764586 9.77730303466667, 50.9818970757127 9.77730303466667))"

For some reason the Polygon Object can not be created.
Is this the wrong way to create Polygon Objects from a list of CLLocationCoordinate2D?
What I am doing wrong? I would really appreciate if you guys can help me with that.

Thanks in advance.

@vfn
Copy link
Member

vfn commented Nov 30, 2017

@SebastianBoldt

  1. longitude precedes latitude
  2. There should be a space in between POLYGON and the first parenthesis ( => "POLYGON (..."

For more info on WKT: https://en.wikipedia.org/wiki/Well-known_text

@SebastianBoldt
Copy link
Author

I fixed the issues you addressed but it still does not work for some reason.
Here is the string im using to initialize the Polygon:

"POLYGON ((9.68879297200007 51.2438481304412, 9.61292720400002 51.3071279038959, 9.47383996266669 51.4570682146156, 9.29681983733338 51.7554766499544, 9.24624265866675 51.8727299411438, 9.20830977466668 52.0130314107855, 9.20830977466668 52.0519261862681, 9.20830977466668 52.0674746238154, 9.20830977466668 52.0752468131056, 9.20830977466668 52.0985552641519, 9.24624265866675 52.2304066207189, 9.3221084266667 52.3541453437191, 9.4106184893334 52.5006364072735, 9.47383996266669 52.5544841276211, 9.499128552 52.5929063739417, 9.52441714133342 52.5929063739417, 9.53706143599998 52.5929063739417, 9.56235002533339 52.5929063739417, 9.58763861466671 52.5775415155444, 9.62557149866668 52.5391058009807, 9.66350438266665 52.5083329809305, 9.71408156133338 52.4775386000022, 9.76465874000001 52.4467226519907, 9.86581309733338 52.4235965338629, 10.1313432853334 52.3773078823186, 10.4095177680001 52.3232430657399, 10.5738935986667 52.2768491510004, 10.7003365453334 52.2226614710605, 10.7382694293334 52.1839154544977, 10.7635580186667 52.1606516265648, 10.8014909026667 52.0907871331812, 10.8394237866667 52.0052483952469, 10.864712376 51.9507293778829, 10.8900009653333 51.9039459736845, 10.8900009653333 51.8493036870028, 10.8900009653333 51.81804970387, 10.8900009653333 51.763303028817, 10.8900009653333 51.6928167743976, 10.8773566706667 51.637918126692, 10.8520680813334 51.5750953384108, 10.8141351973333 51.512185598856, 10.788846608 51.4649462056936, 10.7635580186667 51.4334260815772, 10.750913724 51.4255426503441, 10.7382694293334 51.4176578588585, 10.71298084 51.4097717070348, 10.7003365453334 51.401884194788, 10.6497593666666 51.3939953220327, 10.6118264826667 51.3782134946566, 10.5865378933334 51.3703205398658, 10.5486050093334 51.3703205398658, 10.5233164200001 51.3624262242268, 10.4727392413333 51.3545305476548, 10.2830748213334 51.2913161313519, 10.1186989906666 51.2359320297663, 10.0175446333334 51.204254003468, 9.97961174933329 51.1963310905783, 9.96696745466674 51.1963310905783, 9.95432315999998 51.1963310905783, 9.94167886533342 51.1963310905783, 9.92903457066666 51.204254003468, 9.916390276 51.204254003468, 9.90374598133335 51.204254003468, 9.89110168666669 51.2121755537486, 9.89110168666669 51.2200957415031, 9.87845739200003 51.2280145668146, 9.85316880266672 51.2280145668146, 9.84052450800006 51.2280145668146, 9.81523591866664 51.2280145668146, 9.80259162399999 51.2359320297663, 9.80259162399999 51.2438481304412, 9.78994732933333 51.2438481304412, 9.78994732933333 51.2359320297663, 9.77730303466667 51.2280145668146, 9.77730303466667 51.2200957415031))"

@SebastianBoldt
Copy link
Author

SebastianBoldt commented Nov 30, 2017

@vfn
If I convert the latitude and longitude values to Integers the creation of the Polygon seems to work. Could it be that the Polygon Initializer has some Problems with values like 9.77730303466667 ?

@vfn
Copy link
Member

vfn commented Nov 30, 2017

@SebastianBoldt it works because then your first and last point will be the same, closing the polygon. You must add the first point as the last one to make a closed shape/polygon

@vfn
Copy link
Member

vfn commented Nov 30, 2017

var pairs: [String] = coordinates.flatMap { String("\($0.longitude)) \($0.latitude)")}
pairs.append(pairs[0])

@SebastianBoldt
Copy link
Author

Ok, now I get it. Everything works as expected.
Thanks for you help. I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants