join/merge csv-file & shapefile

1887
1
04-14-2019 04:30 AM
MichaelStöckli
New Contributor

I managed to load a shapefile and color the the layer based on the value of the field "KTNR" with following code: 

// Instantiate a map using a basemap.
map = AGSMap()

// Create a shapefile feature table from a named bundle resource.
let shapefileTable = AGSShapefileFeatureTable(name: "g1g14vz")

// Create a feature layer for the shapefile feature table.
featureLayer = AGSFeatureLayer(featureTable: shapefileTable)

let lineSymbol = AGSSimpleLineSymbol(style: .solid, color: UIColor(white: 0.6, alpha: 1), width: 0.5)
let symbol1 = AGSSimpleFillSymbol(style: .solid, color: .yellow, outline: lineSymbol)
let symbol2 = AGSSimpleFillSymbol(style: .solid, color: .red, outline: lineSymbol)
let symbol3 = AGSSimpleFillSymbol(style: .solid, color: .blue, outline: lineSymbol)

let classBreak1 = AGSClassBreak(description: "0 to 5", label: "0 to 5", minValue: 0, maxValue: 5, symbol: symbol1)
let classBreak2 = AGSClassBreak(description: "5 to 10", label: "5 to 10", minValue: 5, maxValue: 10, symbol: symbol2)
let classBreak3 = AGSClassBreak(description: "10 to 30", label: "10 to 30", minValue: 10, maxValue: 30, symbol: symbol3)
let classBreakRenderer = AGSClassBreaksRenderer(fieldName: "KTNR", classBreaks: [ classBreak1, classBreak2, classBreak3])
featureLayer.renderer = classBreakRenderer

// Add the layer to the map.
map.operationalLayers.add(featureLayer)

What I'm now trying to do is using the data from a csv-file as the field for the AGSClassBreaksRenderer instead of "KTNR". After hours of googling and checking the sample iOS ArcGIS projekt I still don't know how to accomplish that. Can I somehow join the data from the csv to the data from the shapefile? Or can I add the csv-data as another layer and then reference that layer in AGSClassBreaksRenderer? Or can I merge the shapefile with the csv-file somehow and create a new layer out of them? Or is there a different approach to my problem?

 

Best regards,

Michael

Tags (2)
0 Kudos
1 Reply
Nicholas-Furness
Esri Regular Contributor

Hi Michael,

I can't think of a direct way to do this yet (essentially you want a join between the Shapefile and a CSV), but it would help to understand the nature of your data. In particular, how is the CSV contents related to the shapefile records, and does the contents of the CSV change frequently or is it pretty static? Also, is the shapefile very large (i.e. do you want to operate directly against the shapefile, or could you work with an in-memory copy of it?).

Depending on the data, one approach you could take would be to create an AGSFeatureCollectionTable from the result of a query against the shapefile using featureCollectionTableWithFeatureSet() (an AGSFeatureQueryResult is an AGSFeatureSet). Create an AGSFeatureCollectionLayer using that. In the layers collection, you'll see a single AGSFeatureLayer that you can set your renderer on.

The AGSFeatureCollectionTable you create would essentially be an in-memory version of the shapefile data in which you could then edit the "KTNR" values based off the CSV contents without modifying the shapefile itself. Or if you prefer to merge/import some other field from the CSV to key your renderer off, you could populate the AGSFeatureCollectionTable slightly more manually using featureCollectionTableWithGeoElements:fields() copying the fields from the Shapefile (or a subset if you don't need them all), and add an additional field that the renderer will be keyed on, populating that from the CSV.

Could you give us some more information on the nature of the CSV and how it relates to the Shapefile?

Nick.

0 Kudos