Hi,
Does anybody get any idea of converting points to line please? I'm thinking to construct a line json from point jsons and then create a line geometry with the method initWithJSON of AGSPolyline... If this is correct, what would be the best way of constructing the line json from the points jsons?
Thanks in Advance.
Shimin
Solved! Go to Solution.
Hi All,
The line json constructed from points worked. It only took 3 seconds to draw lines for 22000 points.
Here is the function in case anybody else needs it:
@IBAction func convertTrackPointToLine(sender: UIBarButtonItem)
{
if self.trackPointLineOn
{
self.trackPointLineOn = false
self.trackPointToLineButton.tintColor = nil
self.mapView.removeMapLayerWithName(TRACKPOINTLINELAYERNAME)
self.trackPointLineGraphicLayer = nil
}
else
{
let query = AGSQuery()
query.outFields = ["TrackID", "TrackPointNo", "Description", "Contractor", "Operation", "OperationDetail"]
query.orderByFields = ["TrackID ASC", "TrackPointNo ASC"]
query.geometry = self.mapView.visibleArea()
query.spatialRelationship = AGSSpatialRelationship.Intersects
query.returnGeometry = true
let trackPointsFeatureTable = self.trackPointsLayer.table
trackPointsFeatureTable.queryResultsWithParameters(query)
{
(features: [AnyObject]!, error: NSError!) -> Void in
if error != nil
{
logAppStatus("Error querying the TrackPoints layer: " + "\(error)", newLine: "\n")
FCNSWUtils.showAlertWithTitle("Query Error", andMessage: "Error querying the TrackPoints layer: " + "\(error).")
}
else
{
let featureSet = AGSFeatureSet(features: features)
let lineJsonPart1 = "{\"paths\":[["
var lineJsonPart2 = ""
let lineJsonPart3 = "]],\"spatialReference\":{\"wkid\":3308}}"
var count = 0
for feature: AGSGDBFeature in featureSet.features as! [AGSGDBFeature]
{
let point : AGSPoint = feature.geometry as! AGSPoint
let pointJsonValue = point.encodeToJSON() as NSDictionary
let x: Double = pointJsonValue.valueForKey("x") as! Double
let y: Double = pointJsonValue.valueForKey("y") as! Double
let pointXY = "[\(x),\(y)]"
if count == 0
{
lineJsonPart2 += pointXY
}
else
{
lineJsonPart2 += "," + pointXY
}
count++
}
let lineJson = lineJsonPart1 + lineJsonPart2 + lineJsonPart3
let lineJsonValue = (lineJson as NSString).ags_JSONValue() as! NSDictionary
let lineGeometry = AGSPolyline(JSON: lineJsonValue as [NSObject : AnyObject], spatialReference: self.mapView.spatialReference)
self.trackPointLineGraphicLayer = AGSGraphicsLayer.graphicsLayer() as! AGSGraphicsLayer
self.mapView.addMapLayer(self.trackPointLineGraphicLayer, withName: TRACKPOINTLINELAYERNAME)
let blueSimpleLineSymbol = AGSSimpleLineSymbol.init(color: UIColor.blueColor())
let blueSimpleLineGraphic = AGSGraphic.graphicWithGeometry(lineGeometry, symbol: blueSimpleLineSymbol, attributes: nil) as! AGSGraphic
self.trackPointLineGraphicLayer.addGraphic(blueSimpleLineGraphic)
self.trackPointLineOn = true
self.trackPointToLineButton.tintColor = UIColor.cyanColor()
}
}
}
}
Cheers,
Shimin
Shimin Cai Can you provide more details about the use case? On what you are trying to achieve, if possible with an example.
Hi Gagandeep,
Thanks for your reply.
We are Forestry Corporation of New South Wales in Australia. We have a tracking app that records a dozer's locations as points in the background, let's say, in a site preparation operation as an example. The image below shows two recorded operation type points Stacking (green) and Ripping (purple)
Now the operators/contractors are asking converting points to line on the fly. "It's looking like lines from track points from within the app is an important thing. Not so much for harvester tracking but for site prep and roads it seems to be essential. I was thinking that the simplest solution would be to allow the user to build lines from the points within his view extent and delete/rebuild them as required."
I have already tried inserting the points into an AGSSketchGraphicLayer and then grabbing it's resulted line geometry to create a line graphic feature and display the feature on top of the points.
The code is working fine if the number of points is less than 1000 roughly. However, more than 1000 points crash the simulator due to running out of allowed memory. There are totally about 22000 points in the above case.
I think constructing line json from point jsons should be much more efficient and wouldn't require that much memory...
Thanks a lot for your help.
Shimin
Hi All,
The line json constructed from points worked. It only took 3 seconds to draw lines for 22000 points.
Here is the function in case anybody else needs it:
@IBAction func convertTrackPointToLine(sender: UIBarButtonItem)
{
if self.trackPointLineOn
{
self.trackPointLineOn = false
self.trackPointToLineButton.tintColor = nil
self.mapView.removeMapLayerWithName(TRACKPOINTLINELAYERNAME)
self.trackPointLineGraphicLayer = nil
}
else
{
let query = AGSQuery()
query.outFields = ["TrackID", "TrackPointNo", "Description", "Contractor", "Operation", "OperationDetail"]
query.orderByFields = ["TrackID ASC", "TrackPointNo ASC"]
query.geometry = self.mapView.visibleArea()
query.spatialRelationship = AGSSpatialRelationship.Intersects
query.returnGeometry = true
let trackPointsFeatureTable = self.trackPointsLayer.table
trackPointsFeatureTable.queryResultsWithParameters(query)
{
(features: [AnyObject]!, error: NSError!) -> Void in
if error != nil
{
logAppStatus("Error querying the TrackPoints layer: " + "\(error)", newLine: "\n")
FCNSWUtils.showAlertWithTitle("Query Error", andMessage: "Error querying the TrackPoints layer: " + "\(error).")
}
else
{
let featureSet = AGSFeatureSet(features: features)
let lineJsonPart1 = "{\"paths\":[["
var lineJsonPart2 = ""
let lineJsonPart3 = "]],\"spatialReference\":{\"wkid\":3308}}"
var count = 0
for feature: AGSGDBFeature in featureSet.features as! [AGSGDBFeature]
{
let point : AGSPoint = feature.geometry as! AGSPoint
let pointJsonValue = point.encodeToJSON() as NSDictionary
let x: Double = pointJsonValue.valueForKey("x") as! Double
let y: Double = pointJsonValue.valueForKey("y") as! Double
let pointXY = "[\(x),\(y)]"
if count == 0
{
lineJsonPart2 += pointXY
}
else
{
lineJsonPart2 += "," + pointXY
}
count++
}
let lineJson = lineJsonPart1 + lineJsonPart2 + lineJsonPart3
let lineJsonValue = (lineJson as NSString).ags_JSONValue() as! NSDictionary
let lineGeometry = AGSPolyline(JSON: lineJsonValue as [NSObject : AnyObject], spatialReference: self.mapView.spatialReference)
self.trackPointLineGraphicLayer = AGSGraphicsLayer.graphicsLayer() as! AGSGraphicsLayer
self.mapView.addMapLayer(self.trackPointLineGraphicLayer, withName: TRACKPOINTLINELAYERNAME)
let blueSimpleLineSymbol = AGSSimpleLineSymbol.init(color: UIColor.blueColor())
let blueSimpleLineGraphic = AGSGraphic.graphicWithGeometry(lineGeometry, symbol: blueSimpleLineSymbol, attributes: nil) as! AGSGraphic
self.trackPointLineGraphicLayer.addGraphic(blueSimpleLineGraphic)
self.trackPointLineOn = true
self.trackPointToLineButton.tintColor = UIColor.cyanColor()
}
}
}
}
Cheers,
Shimin