Is it possible to set features id when saving them to ShapeFileFeatureLayer?
I open shape file create another one and take needed features from first file and want to leave Ids like it was on first file. After saving I get Id like 1,2,3.... and I need them to stay like they were before (25,100,133...), is there a way?
ShapeFileFeatureLayer features id
Hello Hotter,
Thanks for your post, actually the shape id is just some index id we are using, so it is like 1,2,3, if you want to store your own id, you can create a column for that.
Let me know if you have queries.
Regards,
Gary
Well, I want to use GetFeatureByID which would work fast I guess. If I will create another column would it be the same speed to get it?
Hello Hotter,
For your requirement, because you want to search by your own id(store in a common column), so you can use:
Layer.QueryTools.GetFeaturesByColumnValue()
And the speed will be the same.
Regards,
Gary
I did the test and the performance speed is not the same:
GetFeatureById: 73544
GetFeaturesByColumnValue: 402489
So GetFeatureById is like 5 times faster. Any other ideas? Maybe I could change somehow GetFeaturesByColumnValue to get one feature, not collection? Maybe that would increase performance? But how to do that?
Speed test source code: ShapeFileFeatureLayer layer = new ShapeFileFeatureLayer(@"C:\file.shp", ShapeFileReadWriteMode.ReadOnly);
Feature ft;
Collection<Feature> fts;
layer.Open();
Stopwatch sw = new Stopwatch();
sw.Start();
ft = layer.QueryTools.GetFeatureById("100", ReturningColumnsType.AllColumns);
sw.Stop();
Console.WriteLine("GetFeatureById: {0}", sw.ElapsedTicks);
sw.Reset();
sw.Start();
fts = layer.QueryTools.GetFeaturesByColumnValue("ID", "100", ReturningColumnsType.AllColumns);
sw.Stop();
Console.WriteLine("GetFeaturesByColumnValue: {0}", sw.ElapsedTicks);
sw.Reset();
Console.ReadLine();
layer.Close();
Hello Hotter,
I’m sorry for my mistake, yes the performance is different between GetFeatureById and GetFeaturesByColumnValue, because the id is the number of records in shape file(idx file), so it’s really fast when you search with a specific id, it just need to get that one, but GetFeaturesByColumnValue, it need traverse all the records then find the right column value.
For your requirement, we don’t have that API for now, I think it’s possbile to do that but complex:
1. Make a featuresource inherit from shapefilefeaturesource.
2. Every time call buildIndex(), store the id(number) and your id(maybe string, double, anything) into a file(txt, xml) one by one.
3. In the Open(), read this file into memory, then you have the connection between your ids and records number.
4. Override GetFeatureById(), input your ids, find out the correspond number and call base.GetFeatureById(), pass the number in.
Let me know if you have any queries, I’m glad to help.
Regards,
Gary