I want to edit GeoJson file, which I put part of its lines here. How to edit in this way, I have to read the file and change the Landuse
value using the code in the properties.
{"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[45.882982627281955,35.98144306876872],[45.8830448154499,35.98142063110326],[45.883106013386524,35.98143674855534],[45.883177395327635,35.981590195979166],[45.88306057502328,35.98161790966196],[45.882982627281955,35.98144306876872]]]},"properties":{"Code":1,"Landuse":"مسکونی","Longitude":45.8830793043,"latitude":35.9815185013}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[45.88321822952168,35.98143433703011],[45.88329577844585,35.981578778123584],[45.883184747057655,35.98160599975271],[45.883177395327635,35.981590195979166],[45.88313701140243,35.981503383976175],[45.883107851319025,35.981440699498734],[45.88321822952168,35.98143433703011]]]},"properties":{"Code":2,"Landuse":"مسکونی","Longitude":45.8832014571,"latitude":35.9815182472}},
...
]}
I converted the GeoJson file to C# classes using this site. And the result is as follows
public class ConvertorJsonLayerDTO {
public class Feature {
public string type {
get;
set;
}
public Geometry geometry {
get;
set;
}
public Properties properties {
get;
set;
}
}
public class Geometry {
public string type {
get;
set;
}
public List<List<List<double>>> coordinates {
get;
set;
}
}
public class Properties {
public int Code {
get;
set;
}
public string Landuse {
get;
set;
}
public double Longitude {
get;
set;
}
public double latitude {
get;
set;
}
}
public class Root {
public string type {
get;
set;
}
public List<Feature>features {
get;
set;
}
}
}
Now I read the file in C# as follows:
var code = 2;
var Geojson = File.ReadAllText(Path);
var deserialize = JsonConvert.DeserializeObject<Root>(Geojson);
Now, how do I make a blind move on this file and change the property whose code is 2 to the Landuse
property value and update the file?
Please guide me. I will definitely share the result with you...Thankful
CodePudding user response:
I'd recomend using Linq
:
var feature = deserialize.features.FirstOrDefault(feature => feature.properties.Code == 2);
if (feature != null)
{
feature.properties.Landuse = "new Landuse";
}
It gives you the first feature where feature.properties.Code
is equal to 2
or it returns null
if there is no feature with the code 2.
CodePudding user response:
You could Try with JObject to avoid unnecessary models/Properties for rest part of your file
var str= System.IO.File.ReadAllText("path");
var jobj= JObject.Parse(str);
var newjobj= (JObject)jobj["SomeSection"]["ChildSection"];
var obj = newjobj.ToObject<TargetObject>();