public static LayerMetadata ReadLayerMetadata( JsonObject json ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); LayerMetadata lyr = new LayerMetadata(); lyr.ID = (int)GetRequiredEntryValue( json, LYR_ID ); lyr.Name = (string)GetRequiredEntryValue( json, LYR_NAME ); lyr.Type = (string)GetRequiredEntryValue( json, LYR_TYPE ); lyr.GeometryType = ( string ) GetRequiredEntryValue( json, LYR_GEOMETRY_TYPE ); lyr.Description = ( string ) GetRequiredEntryValue( json, LYR_DESCRIPTION ); lyr.DefinitionExpression = ( string ) GetRequiredEntryValue( json, LYR_DEFINITION_EXPRESSION ); lyr.CopyrightText = ( string ) GetRequiredEntryValue( json, LYR_COPYRIGHT_TEXT ); lyr.MinimumScale = ( double ) GetRequiredEntryValue( json, LYR_MINIMUM_SCALE ); lyr.MaximumScale = ( double ) GetRequiredEntryValue( json, LYR_MAXIMUM_SCALE ); string extent_entry = GetMatchingKey( json, LYR_EXTENT ); if( string.IsNullOrEmpty( extent_entry ) ) throw new FormatException( "An extent member is required in the layer metadata object." ); if( !( json[ LYR_EXTENT ] is JsonObject ) ) throw new FormatException( "The extent member should be of type JsonObject." ); lyr.Extent = ReadEnvelope( json[ LYR_EXTENT ] as JsonObject); lyr.DisplayField = ( string ) GetRequiredEntryValue( json, LYR_DISPLAY_FIELD ); string fields_entry = GetMatchingKey( json, LYR_FIELDS ); if( string.IsNullOrEmpty( fields_entry ) ) throw new FormatException( "A fields member is required in the layer metadata object." ); if( json[ LYR_FIELDS ] != null ) { if( !( json[ LYR_FIELDS ] is JsonArray ) ) throw new FormatException( "The fields member should be of type JsonArray." ); lyr.Fields = ReadFieldMetadataArray( json[ LYR_FIELDS ] as JsonArray ); } string parentLayer_entry = GetMatchingKey( json, LYR_PARENT_LAYER ); if( string.IsNullOrEmpty( parentLayer_entry ) ) throw new FormatException( "An parentLayer member is required in the layer metadata object." ); if(json[LYR_PARENT_LAYER] != null) { if( !( json[ LYR_PARENT_LAYER ] is JsonObject ) ) throw new FormatException( "The parentLayer member should be of type JsonObject." ); lyr.ParentLayer = ReadLayerReference( json[ LYR_PARENT_LAYER ] as JsonObject); } string subLayers_entry = GetMatchingKey( json, LYR_SUBLAYERS ); if( string.IsNullOrEmpty( subLayers_entry ) ) throw new FormatException( "A subLayers member is required in the layer metadata object." ); if( json[ LYR_SUBLAYERS ] != null ) { if( !( json[ LYR_SUBLAYERS ] is JsonArray ) ) throw new FormatException( "The subLayers member should be of type JsonArray." ); lyr.SubLayers = ReadLayerReferenceArray( json[ LYR_SUBLAYERS ] as JsonArray ); } return lyr; }
public static List<Graphic> ReadGeoJSON( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); if( !jsonObject.Keys.Contains( TYPE_IDENTIFIER ) ) throw new InvalidOperationException( "The GeoJSON object must contain a VALID type property." ); string type = ( string ) jsonObject[ TYPE_IDENTIFIER ]; if( !GEOJSON_TYPES.Contains<string>( type ) ) throw new InvalidOperationException( "The type property value '" + type + "' is not a valid GeoJSON type." ); if( type == FEATURE_COLLECTION_TYPE_NAME ) return ReadFeatures( jsonObject ); if( type == FEATURE_TYPE_NAME ) return ReadFeature( jsonObject ); if( GEOMETRY_TYPES.Contains<string>( type ) ) { List<Geometry> geometries = ReadGeometry( jsonObject ); List<Graphic> graphics = new List<Graphic>(); foreach( Geometry item in geometries ) { graphics.Add( new Graphic() { Geometry = item, Symbol = item.GetDefaultSymbol() } ); } return graphics; } throw new InvalidOperationException( "The type property value '" + type + "' is not a valid GeoJSON type." ); }
public static List<Graphic> ReadFeatures( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); if( !jsonObject.Keys.Contains( TYPE_IDENTIFIER ) ) throw new InvalidOperationException( "FeatureCollection objects should contain a VALID feature 'type' property value." ); if( ( string ) jsonObject[ TYPE_IDENTIFIER ] != FEATURE_COLLECTION_TYPE_NAME ) throw new InvalidOperationException( "The object is not of type 'FeatureCollection'. It is of type '" + ( string ) jsonObject[ FEATURE_IDENTIFIER ] + "'." ); if( !jsonObject.Keys.Contains( FEATURES_IDENTIFIER ) ) throw new InvalidOperationException( "The FeatureCollection object must contain a 'features' property." ); if( !( jsonObject[ FEATURES_IDENTIFIER ].JsonType == JsonType.Array ) ) throw new InvalidOperationException( "The value of the features property of a FeatureCollection object should be an array." ); JsonArray features = jsonObject[ FEATURES_IDENTIFIER ] as JsonArray; List<Graphic> graphics = new List<Graphic>(); foreach( JsonObject item in features ) { List<Graphic> list = ReadFeature(item); graphics.AddRange( list ); } return graphics; }
public static Geometry ReadIndividualGeometry( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); string geometryType = ( string ) jsonObject[ TYPE_IDENTIFIER ]; if( string.IsNullOrEmpty( geometryType ) ) throw new InvalidOperationException( "Geometry objects should contain a VALID geometry 'type' property value." ); if( geometryType == GEOMETRY_COLLECTION_TYPE_NAME ) throw new InvalidOperationException( "The object is a 'GeometryCollection' and not a 'Geometry'." ); JsonArray coordinates = ( JsonArray ) jsonObject[ COORDINATES_IDENTIFIER ]; if( coordinates == null ) throw new InvalidOperationException( "Geometry objects should contain a valid 'coordinates' array." ); Geometry geom = null; switch( geometryType ) { case POINT_TYPE_NAME: geom = ReadPoint( coordinates ); break; case MULTIPOINT_TYPE_NAME: geom = ReadMultiPoint( coordinates ); break; case LINESTRING_TYPE_NAME: Polyline line = new Polyline(); line.Paths.Add( ReadLineString( coordinates ) ); geom = line; break; case MULTILINESTRING_TYPE_NAME: geom = ReadMultiLineString( coordinates ); break; case POLYGON_TYPE_NAME: geom = ReadPolygon( coordinates ); break; case MULTIPOLYGON_TYPE_NAME: geom = ReadMultiPolygon( coordinates ); break; default: break; } return geom; }
public static List<Geometry> ReadGeometry( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); if( !jsonObject.Keys.Contains( TYPE_IDENTIFIER ) ) throw new InvalidOperationException( "Geometry objects should contain a VALID geometry 'type' property value." ); string type = (string)jsonObject[ TYPE_IDENTIFIER ]; if( type == GEOMETRY_COLLECTION_TYPE_NAME ) { return ReadGeometryCollection( jsonObject ); } else { return new List<Geometry>(){ ReadIndividualGeometry( jsonObject ) }; } return null; }
public static Dictionary<string, object> ReadProperties( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); Dictionary<string, object> properties = new Dictionary<string, object>(); foreach( string key in jsonObject.Keys ) { if( jsonObject[ key ].JsonType == JsonType.String ) properties.Add( key, ( string ) jsonObject[ key ] ); else if( jsonObject[ key ].JsonType == JsonType.Number ) properties.Add( key, ( double ) jsonObject[ key ] ); else if( jsonObject[ key ].JsonType == JsonType.Boolean ) properties.Add( key, ( bool ) jsonObject[ key ] ); else properties.Add(key, jsonObject[key]); } return properties; }
public static List<Graphic> ReadFeature( JsonObject jsonObject ) { jsonObject.RequireArgument<JsonObject>( "jsonObject" ).NotNull<JsonObject>(); if( !jsonObject.Keys.Contains( TYPE_IDENTIFIER ) ) throw new InvalidOperationException( "Feature objects should contain a VALID feature 'type' property value." ); if( (string)jsonObject[ TYPE_IDENTIFIER ] != FEATURE_TYPE_NAME ) throw new InvalidOperationException( "The object is not of type 'Feature'. It is of type '" + (string)jsonObject[FEATURE_IDENTIFIER] + "'." ); if( !jsonObject.Keys.Contains( GEOMETRY_IDENTIFIER ) ) throw new InvalidOperationException( "The feature object must contain a 'geometry' property." ); if( !jsonObject.Keys.Contains( PROPERTIES_IDENTIFIER ) ) throw new InvalidOperationException( "The feature object must contain a 'properties' property." ); string id = string.Empty; if( jsonObject.Keys.Contains( ID_IDENTIFIER ) ) { id = ( string ) jsonObject[ ID_IDENTIFIER ]; } List<Geometry> shapes = ReadGeometry( jsonObject[ GEOMETRY_IDENTIFIER ] as JsonObject); Dictionary<string, object> properties = ReadProperties(jsonObject[PROPERTIES_IDENTIFIER] as JsonObject); List<Graphic> graphics = new List<Graphic>(); foreach( Geometry entry in shapes ) { Graphic graphic = new Graphic() { Geometry = entry, Symbol = entry.GetDefaultSymbol() }; foreach( var item in properties ) { graphic.Attributes.Add( item.Key, item.Value ); } if( !string.IsNullOrEmpty( id ) ) graphic.Attributes.Add(ATTRIBUTES_FEATURE_IDENTIFIER, id); graphics.Add( graphic ); } return graphics; }
public static string GetMatchingKey( JsonObject json, string key, bool isCaseSensitive ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); key.RequireArgument<string>( "key" ).NotNullOrEmpty(); StringComparison sc = isCaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase; foreach( string entry in json.Keys ) { if( string.Compare( entry, key, sc ) == 0 ) return entry; } return null; }
public static SpatialReference ReadSpatialReference( JsonObject json ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); SpatialReference sr = new SpatialReference(); string wkid_entry = GetMatchingKey( json, SPATIALREFERENCE_WKID ); //Read the WKID value if it is present //If WKID is not present, do not throw an exception here for now //because a lot of times, the Spatial reference is an empty object if the user doesn't set it specifically if( !string.IsNullOrEmpty( wkid_entry ) ) sr.WKID = (int)json[ wkid_entry ]; return sr; }
public static Envelope ReadEnvelope( JsonObject json ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); Envelope env = new Envelope(); env.XMin = ( double ) GetRequiredEntryValue(json, GEOMETRY_ENVELOPE_XMIN); env.YMin = ( double ) GetRequiredEntryValue(json, GEOMETRY_ENVELOPE_YMIN); env.XMax = ( double ) GetRequiredEntryValue(json, GEOMETRY_ENVELOPE_XMAX); env.YMax = ( double ) GetRequiredEntryValue( json, GEOMETRY_ENVELOPE_YMAX ); string sr_entry = GetMatchingKey( json, GEOMETRY_SPATIALREFERENCE); if( string.IsNullOrEmpty( sr_entry ) ) throw new FormatException( string.Format( "A {0} member is expected in a Envelope object.", GEOMETRY_SPATIALREFERENCE ) ); if( json[ sr_entry ] == null ) return env; if( !( json[ sr_entry ] is JsonObject ) ) throw new FormatException( "The Spatial reference member should be of type JsonObject" ); env.SpatialReference = ReadSpatialReference( json[sr_entry] as JsonObject ); return env; }
public static KeyValuePair<int, string> ReadLayerReference( JsonObject json ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); int id = (int)GetRequiredEntryValue( json, LYR_LAYER_REF_ID ); string name = ( string ) GetRequiredEntryValue( json, LYR_LAYER_REF_NAME ); KeyValuePair<int, string> lyrRef = new KeyValuePair<int, string>(id, name); return lyrRef; }
public static FieldMetadata ReadFieldMetadata( JsonObject json ) { json.RequireArgument<JsonObject>( "json" ).NotNull<JsonObject>(); FieldMetadata fldMetadata = new FieldMetadata(); fldMetadata.Name = ( string ) GetRequiredEntryValue( json, FIELD_NAME ); fldMetadata.Type = ( string ) GetRequiredEntryValue( json, FIELD_TYPE ); fldMetadata.Alias = ( string ) GetRequiredEntryValue( json, FIELD_ALIAS ); return fldMetadata; }