How to parse DbGeometry object into List<T>? - c#

I'm trying to parse a polygon, which is presented as DbGeometry class (from the System.Data.Entity.Spatial) to some List representation, but there were fails.
I've tried:
- to convert it with the method .ToList<>()
- to parse using JSON library in .NET, but have sample code from different websites failed with deserializing DbGeometry
So, right now I'm returning geometry as a string in my Web API application:
If I couldn't find any solution how, to represent it as list of doubles I will have to parse manually it in JavaScript, which way I think is very incorrect and there must be some solution.
I'm using Entity Framework v.6.1.1, which has prepared the next model:
public partial class Buildings
{
public int id { get; set; }
public Nullable<bool> has_holes { get; set; }
public System.Data.Entity.Spatial.DbGeometry polygon_data { get; set; }
public System.Data.Entity.Spatial.DbGeometry position_wgs { get; set; }
public System.Data.Entity.Spatial.DbGeometry position_mercator { get; set; }
public Nullable<int> height { get; set; }
public string street_name { get; set; }
public System.Data.Entity.Spatial.DbGeometry holes_data { get; set; }
public Nullable<double> angle { get; set; }
}
I've shown it, if you want to see a structure of the table from MSSQL CE (which is an embedded db, or LocalDb, another name).
So I want:
System.Data.Entity.Spatial.DbGeometry polygon_data
System.Data.Entity.Spatial.DbGeometry holes_data
Be prepared as lists of doubles, so my question is: How can I parse DbGeometry instance, which holds a collection of points into List<Point>?.

I had similar problem. What I did was, created extension method that parse the given geometry data to points. #Erik Philips also have nice solution though. This is what I came up with.
public static class ExtensionString
{
private static Dictionary<string, string> _replacements = new Dictionary<string, string>();
static ExtensionString()
{
_replacements["LINESTRING"] = "";
_replacements["CIRCLE"] = "";
_replacements["POLYGON"] = "";
_replacements["POINT"] = "";
_replacements["("] = "";
_replacements[")"] = "";
}
public static List<Point> ParseGeometryData(this string s)
{
var points = new List<Point>();
foreach (string to_replace in _replacements.Keys)
{
s = s.Replace(to_replace, _replacements[to_replace]);
}
string[] pointsArray = s.Split(',');
for (int i = 0; i < pointsArray.Length; i++)
{
double[] coordinates = new double[2];
//gets x and y coordinates split by space, trims whitespace at pos 0, converts to double array
coordinates = Array.ConvertAll(pointsArray[i].Remove(0, 1).Split(null), double.Parse);
points.Add(new Point() { X = coordinates[0], Y = coordinates[1] });
}
return points;
}
}
And just use it like this
List<System.Drawing.Point> points = geometryDataStr.ParseGeometryData();

If your Geometry is Valid then you can do:
class Program
{
static void Main(string[] args)
{
DbGeometry test = DbGeometry.FromText("POLYGON((1 1, 1 2, 3 3, 1 1))");
var foo = test.AsText();
var points = new List<Point>();
Console.WriteLine(foo);
if (foo.StartsWith("POLYGON ((")
&& foo.EndsWith("))"))
{
foo = foo.Substring(10, foo.Length - 12);
var rawPoints = foo.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (var rawPoint in rawPoints)
{
var splitPoint = rawPoint.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
points.Add(new Point() { X = decimal.Parse(splitPoint[1]), Y = decimal.Parse(splitPoint[0]) });
}
}
foreach (var point in points)
{
Console.WriteLine(point.ToString());
}
Console.ReadKey();
}
}
class Point
{
public decimal X { get; set; }
public decimal Y { get; set; }
public override string ToString()
{
return string.Format("[X={0}],[Y={1}]", X, Y);
}
}
result:
POLYGON ((1 1, 1 2, 3 3, 1 1))
[X=1],[Y=1]
[X=2],[Y=1]
[X=3],[Y=3]
[X=1],[Y=1]

Related

Resampling with Deedle duplicate keys

My code below resamples 5-minute interval to 1-day interval for the daily profit stats. The problem is that BacktestResult consists of duplicate CloseDate values, because I'm testing with multiple pairs (TRXUSDT, ETHUSDT and BTCUSDT). dailyProfit returns Series<DateTime, double>, which explains the exception. How can I make it grouped by Pair or something? It works fine when tested with one pair.
// Create series
var series = _backtestResults.ToOrdinalSeries();
// daily_profit = results.resample('1d', on = 'close_date')['profit_percent'].sum()
var dailyProfit = series.ResampleEquivalence(
index => new DateTime(series[index].CloseDate.Year, series[index].CloseDate.Month, series[index].CloseDate.Day, 0, 0, 0, DateTimeKind.Utc),
group => group.SelectValues(g => g.ProfitPercentage).Sum()).DropMissing();
// classes
public class BacktestResult
{
public string Pair { get; set; }
public decimal ProfitPercentage { get; set; }
public decimal ProfitAbs { get; set; }
public decimal OpenRate { get; set; }
public decimal CloseRate { get; set; }
public DateTime OpenDate { get; set; }
public DateTime CloseDate { get; set; }
public decimal OpenFee { get; set; }
public decimal CloseFee { get; set; }
public decimal Amount { get; set; }
public decimal TradeDuration { get; set; }
public SellType SellReason { get; set; }
}
Edit:
Example which takes the JSON data from pastebin:
using Deedle;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace Resample
{
class Program
{
public class BacktestResultTest
{
public string Pair { get; set; }
public decimal ProfitPercentage { get; set; }
public decimal ProfitAbs { get; set; }
public decimal OpenRate { get; set; }
public decimal CloseRate { get; set; }
public DateTime OpenDate { get; set; }
public DateTime CloseDate { get; set; }
public decimal OpenFee { get; set; }
public decimal CloseFee { get; set; }
public decimal Amount { get; set; }
public decimal TradeDuration { get; set; }
public bool OpenAtEnd { get; set; }
public int SellReason { get; set; }
}
static void Main(string[] args)
{
// Take JSON data from pastebin
using var webClient = new WebClient();
var json = webClient.DownloadString("https://pastebin.com/raw/Dhp9202f");
// Deserialize the data
var data = JsonConvert.DeserializeObject<List<BacktestResultTest>>(json);
var ts = data.ToOrdinalSeries();
var byDateAndPair = ts.SelectKeys(kvp => Tuple.Create(kvp.Value.Value.CloseDate, kvp.Value.Value.Pair)).SortByKey();
// daily_profit = results.resample('1d', on = 'close_date')['profit_percent'].sum()
var dailyProfit2 = byDateAndPair.ResampleEquivalence(
k => Tuple.Create(new DateTime(k.Item1.Year, k.Item1.Month, k.Item1.Day), k.Item2),
g => g.Select(kvp => kvp.Value.ProfitPercentage).Sum());
// backtest_worst_day = min(daily_profit)
var worstDay2 = dailyProfit2.Min();
// backtest_best_day = max(daily_profit)
var bestDay2 = dailyProfit2.Max();
// winning_days = sum(daily_profit > 0)
var winningDays2 = dailyProfit2.SelectValues(x => x > 0).Sum();
// draw_days = sum(daily_profit == 0)
var drawDays2 = dailyProfit2.SelectValues(x => x == 0).Sum();
// losing_days = sum(daily_profit < 0)
var losingDays2 = dailyProfit2.SelectValues(x => x < 0).Sum();
Console.ReadLine();
}
}
}
You can use a custom data type as a key in Deedle. If you want to be able to use resampling on the series, then this needs to support IComparable. You can either define your own type or use built-in Tuple.
Assuming we have some very basic data:
var ts =
new[] {
KeyValue.Create(new DateTime(2020,1,1), new { Value = 1.0, Kind = "A" }),
KeyValue.Create(new DateTime(2020,1,2), new { Value = 1.0, Kind = "A" }),
KeyValue.Create(new DateTime(2020,1,3), new { Value = 1.0, Kind = "B" }),
KeyValue.Create(new DateTime(2020,1,4), new { Value = 1.0, Kind = "B" }),
}.ToSeries();
The first thing we need to do is to change the key to be the date together with a kind. (In fact, you can get into trouble earlier in your code if you had duplicate dates!)
var byDateAndKind =
ts.SelectKeys(kvp => Tuple.Create(kvp.Key, kvp.Value.Value.Kind)).SortByKey();
Now the key is Tuple<DateTime, string> consisting of the date and the kind. You can now use ResampleEquivalence on this. Here, we use year and kind as the new key and sum values in group:
var aggByYearAndKind =
byDateAndKind.ResampleEquivalence(
(k) => Tuple.Create(k.Item1.Year, k.Item2),
(g) => g.Select(kvp => kvp.Value.Value).Sum());
aggByYearAndKind.Print();
This will print a series that maps 2020, "A" to 2 and also 2020, "B" to 2.
EDIT You are right - this does not seem to work. I was able to get it to work using GroupBy instead of ResampleEquvialence:
var dailyProfit2 =
ts.GroupBy(kvp =>
new { Date = new DateTime(kvp.Value.CloseDate.Year, kvp.Value.CloseDate.Month, kvp.Value.CloseDate.Day), Kind = kvp.Value.Pair })
.SelectValues(g => g.Select(kvp => kvp.Value.ProfitPercentage).Values.Sum());
// backtest_worst_day = min(daily_profit)
var worstDay2 = dailyProfit2.Min();
// backtest_best_day = max(daily_profit)
var bestDay2 = dailyProfit2.Max();
// winning_days = sum(daily_profit > 0)
var winningDays2 = dailyProfit2.Where(x => x.Value > 0).Values.Sum();
// draw_days = sum(daily_profit == 0)
var drawDays2 = dailyProfit2.Where(x => x.Value == 0).Values.Sum();
// losing_days = sum(daily_profit < 0)
var losingDays2 = dailyProfit2.Where(x => x.Value < 0).Values.Sum();

Map one class data to another class with iteration

I have a C# project and looking for simple solution for map one class object data to list of another class object.
This is my input class
public class RatesInput
{
public string Type1 { get; set; }
public string Break1 { get; set; }
public string Basic1 { get; set; }
public string Rate1 { get; set; }
public string Type2 { get; set; }
public string Break2 { get; set; }
public string Basic2 { get; set; }
public string Rate2 { get; set; }
public string Type3 { get; set; }
public string Break3 { get; set; }
public string Basic3 { get; set; }
public string Rate3 { get; set; }
}
This is my another class structure
public class RateDetail
{
public string RateType { get; set; }
public decimal Break { get; set; }
public decimal Basic { get; set; }
public decimal Rate { get; set; }
}
it has a object like below. (For easiering the understanding, I use hardcoded values and actually values assign from a csv file)
RatesInput objInput = new RatesInput();
objInput.Type1 = "T";
objInput.Break1 = 100;
objInput.Basic1 = 50;
objInput.Rate1 = 0.08;
objInput.Type2 = "T";
objInput.Break2 = 200;
objInput.Basic2 = 50;
objInput.Rate2 = 0.07;
objInput.Type3 = "T";
objInput.Break3 = 500;
objInput.Basic3 = 50;
objInput.Rate3 = 0.06;
Then I need to assign values to "RateDetail" list object like below.
List<RateDetail> lstDetails = new List<RateDetail>();
//START Looping using foreach or any looping mechanism
RateDetail obj = new RateDetail();
obj.RateType = //first iteration this should be assigned objInput.Type1, 2nd iteration objInput.Type2 etc....
obj.Break = //first iteration this should be assigned objInput.Break1 , 2nd iteration objInput.Break2 etc....
obj.Basic = //first iteration this should be assigned objInput.Basic1 , 2nd iteration objInput.Basic2 etc....
obj.Rate = //first iteration this should be assigned objInput.Rate1, 2nd iteration objInput.Rate2 etc....
lstDetails.Add(obj); //Add obj to the list
//END looping
Is there any way to convert "RatesInput" class data to "RateDetail" class like above method in C#? If yes, how to iterate data set?
Try this:
public class RatesList : IEnumerable<RateDetail>
{
public RatesList(IEnumerable<RatesInput> ratesInputList)
{
RatesInputList = ratesInputList;
}
private readonly IEnumerable<RatesInput> RatesInputList;
public IEnumerator<RateDetail> GetEnumerator()
{
foreach (var ratesInput in RatesInputList)
{
yield return new RateDetail
{
RateType = ratesInput.Type1,
Break = Convert.ToDecimal(ratesInput.Break1, new CultureInfo("en-US")),
Basic = Convert.ToDecimal(ratesInput.Basic1, new CultureInfo("en-US")),
Rate = Convert.ToDecimal(ratesInput.Rate1, new CultureInfo("en-US"))
};
yield return new RateDetail
{
RateType = ratesInput.Type2,
Break = Convert.ToDecimal(ratesInput.Break2),
Basic = Convert.ToDecimal(ratesInput.Basic2),
Rate = Convert.ToDecimal(ratesInput.Rate2, new CultureInfo("en-US"))
};
yield return new RateDetail
{
RateType = ratesInput.Type3,
Break = Convert.ToDecimal(ratesInput.Break3),
Basic = Convert.ToDecimal(ratesInput.Basic3),
Rate = Convert.ToDecimal(ratesInput.Rate3, new CultureInfo("en-US"))
};
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
And use:
var list = new RatesList(new List<RatesInput>() { objInput });
foreach (var item in list)
{
Console.WriteLine(item.Basic);
}
You can use Reflection to get the properties info like this:
var props = objInput.GetType().GetProperties();
var types = props.Where(x => x.Name.StartsWith("Type"))
.Select(x => x.GetValue(objInput)).ToList();
var breaks = props.Where(x => x.Name.StartsWith("Break"))
.Select(x => x.GetValue(objInput)).ToList();
var basics = props.Where(x => x.Name.StartsWith("Basic"))
.Select(x => x.GetValue(objInput)).ToList();
var rates = props.Where(x => x.Name.StartsWith("Rate"))
.Select(x => x.GetValue(objInput)).ToList();
List<RateDetail> lstDetails = new List<RateDetail>();
for (int i = 0; i < types.Count; i++)
{
lstDetails.Add(new RateDetail
{
RateType = types[i].ToString(),
Break = Convert.ToDecimal(breaks[i]),
Basic = Convert.ToDecimal(basics[i]),
Rate = Convert.ToDecimal(rates[i])
});
}

Iterate through array and add up elements with a control break logic

I am trying to use this for each loop to iterate through this array, and sum up the elements, 12, 10, 8, 7, and 6, when element 0 is not different (position.account), using control break logic,
This is furthering on a question I had earlier, but I can't seem to figure out how to do this logically.
static void Main(string[] args)
{
String path = #"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CsvReader\Position_2016_02_25.0415.csv";
//Adding lines read into a string[];
string[] lines = File.ReadAllLines(path);
foreach(string line in lines)
{
Positions position = new Positions();
string[] parsedLine = line.Split(',');
position.account = parsedLine[0];
position.settleMM = parsedLine[10];
position.open = parsedLine[6];
position.buy = parsedLine[7];
position.sell = parsedLine[8];
position.underlying = parsedLine[12];
//Need to convert these to an int.
//for each iteration through the loop where string[0] is already existing
//I want to have sum = sum + string[10]
}
Console.Read();
}
public class Positions
{
public string account { get; set; }
public string symbol { get; set; }
public string prevClose { get; set; }
public string curPrx { get; set; }
public string settlePX { get; set; }
public string Mult { get; set; }
public string open { get; set; }
public string buy { get; set; }
public string sell { get; set; }
public string netMM { get; set; }
public string settleMM { get; set; }
public string settleDay { get; set; }
public string underlying { get; set; }
}
Further to my comment, you could do something like this:
// store the accounts inside this dictionary
var accounts = new Dictionary<string, Positions>();
foreach(string line in lines)
{
Positions position = new Positions();
string[] parsedLine = line.Split(',');
position.account = parsedLine[0];
...
Positions existingAccount;
// if the account already exists in the dictionary
if (accounts.TryGetValue(position.account, out existingAccount)) {
existingAccount.buy += position.buy;
// do updating logic here
} else {
accounts.add(position.account, position);
// otherwise add it as a new element
}
}
Alternatively, you could go for Linq:
File.ReadLines(path)
.Select( line => new Position(line) )
.GroupBy( position => position.account )
.Select( group => new { Account = group.Key,
Sum = group.Select( position => position.settleMM ).Sum() } );

C# Deserialize List<T> Issue

This is the class I am trying to serialize and deserialize:
[Serializable]
[XmlRoot("RecipeStepWords")]
public class CRecipeStepsWords
{
[XmlArray]
public List<RecipeStep> ReadRecipeSteps { get; set; }
[XmlArray]
public List<RecipeStep> WriteRecipeSteps { get; set; }
public CRecipeStepsWords()
{
ReadRecipeSteps = new List<RecipeStep>();
for (int x = 1; x <= 8; x++)
{
ReadRecipeSteps.Add(new RecipeStep());
}
WriteRecipeSteps = new List<RecipeStep>();
for (int x = 1; x <= 6; x++)
{
WriteRecipeSteps.Add(new RecipeStep());
}
}
public class RecipeStep
{
public String Test1 { get; set; }
public String Test2 { get; set; }
public RecipeStep()
{
Test1= "Sample 1";
Test2 = "Sample 2";
}
}
}
As you can see from the code, ReadRecipeSteps should have 8 items and WriteRecipeSteps should have 6. However, whenever I run the program multiple times, the deserialized list gets added to,i.e., first run = ReadRecipeStesp.Count = 8, second run .Count = 16, etc.
Here is how I deserialize:
if (File.Exists(Path.Combine(path, fName)))
{
StreamReader objStreamReader = new StreamReader(Path.Combine(path, fName));
XmlSerializer x = new XmlSerializer(RecipeStepsWords.GetType());
RecipeStepsWords = (CRecipeStepsWords)x.Deserialize(objStreamReader);
objStreamReader.Close();
}
I'm using VS 2010 for this. What I would like is to always have the correct number of items in the lists, 8 and 6, instead of having them added to every time it gets deserialized. Any help with this would be appreciated.
Try adding an
[XmlArrayItem("ReadReceipe")] right after your xml array. Same for the WriteReciepe
So like this.
[XmlArray]
[XmlArrayItem("ReadReceipe")]
public List<RecipeStep> ReadRecipeSteps { get; set; }
I think what happens is you just keep adding those steps to the root, without having a parent node.
Let me know.
// Reset your object
RecipeStepsWords = new CRecipeStepsWords();
RecipeStepsWords = (CRecipeStepsWords)x.Deserialize(objStreamReader);
objStreamReader.Close();

parse JSON array with json.NET and C#

I have some data in the following JSON format that I need to parse:
{
"status":0,
"timestamp":"8:20pm",
"routes":[
{
"directions":[
"E Towne",
"ETP"
],
"routeID":"30"
},
{
"directions":[
"Johnson",
"Observatory"
],
"routeID":"81"
}
]
}
Using json.net, I have got want to get the following output:
30 E Towne – ETP
81 Johnson – Observatory
Using the code below, I get the following incorrect output:
30 E Towne – ETP
81 E Towne – ETP
How do I write out the directions array items to the corresponding routeID item? My code:
public class Route
{
public string routeID { get; set; }
public string directions { get; set; }
}
private void routeClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string jsonResults_routes = e.Result;
JObject routeData = JObject.Parse(jsonResults_routes);
JArray routeIdArray = (JArray)routeData["routes"];
JArray routeDirections = (JArray)routeData["routes"][0]["directions"];
List<Route> l = new List<Route>();
for (int i = 0; i < routeIdArray.Count; i++)
{
Route BusRoutes = new Route();
JObject routeIDarrayObject = (JObject)routeIdArray[i];
BusRoutes.routeID = (string)routeIDarrayObject["routeID"];
string sep = " - ";
string bothDirections = String.Join(sep, routeDirections);
List<string> sc = new List<string>();
string[] direction = new string[]{bothDirections};
sc.AddRange(direction);
foreach (string direx in sc)
{
BusRoutes.directions = direx;
}
l.Add(BusRoutes);
}
var newList = l.OrderBy(x => x.routeID).ToList();
lbRoutesData.ItemsSource = newList;
}
#competent_tech is correct in is analysis. If I can propose, I think it'll feel more natural if you work with actual objects. For example:
public class RouteInfo
{
public List<string> Directions { get; set; }
public string RouteID { get; set; }
}
public class RouteData
{
public int Status { get; set; }
public string Timestamp { get; set; }
public List<RouteInfo> Routes { get; set; }
}
And in your method:
var routeData = JsonConvert.DeserializeObject<RouteData>(e.Result);
return routeData.Routes
.Select(r => new Route
{
routeId = r.RouteID,
directions = String.Join(" - ", r.Directions)
})
.OrderBy(r =­> r.routeId)
.ToList();
Or manipulate your type object in other ways, more naturally.
Edit: For an easy way to generate classes based on a JSON string, go to json2csharp.
This is because your routeDirections is specifically asking for the first element in the array:
JArray routeDirections = (JArray)routeData["routes"][0]["directions"];
You need to move this logic inside the loop and use the current loop indexer:
for (int i = 0; i < routeIdArray.Count; i++)
{
Route BusRoutes = new Route();
JObject routeIDarrayObject = (JObject)routeIdArray[i];
BusRoutes.routeID = (string)routeIDarrayObject["routeID"];
JArray routeDirections = routeIDarrayObject["directions"];

Categories

Resources