How to convert a DataSet to FeatureDataSet - sharpmap

I am trying to get the geometry data from a dataset to a featuredataset:
private void QueryCustomer(DataSet ds)
{
SharpMap.Data.FeatureDataSet ds_feature = new SharpMap.Data.FeatureDataSet();
ds_feature = (SharpMap.Data.FeatureDataSet)ds; // ERROR HERE
..
I am getting :
Unable to cast object of type 'System.Data.DataSet' to type 'SharpMap.Data.FeatureDataSet'
Any help would be appreciated. Thanks.

No need to create a DataSet. Just get your table directly from SqLite using the FeatureDataSet:
double x, y;
FeatureDataSet fds = new FeatureDataSet();
Envelope env = new Envelope(double.MinValue, double.MaxValue, double.MinValue, double.MaxValue);
SharpMap.Data.Providers.ManagedSpatiaLite p = new ManagedSpatiaLite(ConnectionString, Table, GeometryColumn.ToUpper(), KeyColumn.ToUpper());
p.Open();
p.ExecuteIntersectionQuery(env, fds);
foreach (FeatureDataRow fdr in ((FeatureDataTable)fds.Tables[0]).Rows)
{
x = fdr.Geometry.Centroid.X;
y = fdr.Geometry.Centroid.Y;
//...process x and y here...
}
p.Close();
p.Dispose();

Related

Parameters do not pass to a MySql stored procedure from F#

I'd like to invoke a stored procedure with parameters from F#, but it results in exception "MySql.Data.MySqlClient.MySqlException (0x80004005): Incorrect number of arguments for PROCEDURE test.GetValue; expected 1, got 0".
A similar code in C# works perfectly.
This is .NetCoreApp3.1, FSharp.Core/4.7.0 and MySql.Data/8.0.20. MySql server is 8.0.20, OS is Ubuntu 18.04.4 LTS.
Question: Am I doing sth. wrong or is it a bug?
NOTE: If I remove the parameter from stored procedure and hard-code it in its SELECT query, it works fine in F#. Also, when debugging, I can see the parameters perfectly in place before the call.
F# code that results in exception:
module test_mysql_fsharp.main
open System
open System.Data
open MySql.Data.MySqlClient
[<EntryPoint>]
let main argv =
Console.WriteLine("Hello from test_mysql_fsharp !");
let execStoredProc cs storedProcedureName storedProcedureParameters =
use mySqlConnection = new MySqlConnection(cs)
use command = new MySqlCommand(storedProcedureName, mySqlConnection)
command.CommandType = CommandType.StoredProcedure |> ignore
storedProcedureParameters |> List.iter(fun par -> command.Parameters.AddWithValue(par) |> ignore)
mySqlConnection.Open()
let dataTable = new DataTable()
dataTable.Load(command.ExecuteReader())
dataTable
let cs = "server=127.0.0.1;port=3306;database=test;Uid=***;Pwd=***;"
execStoredProc cs "GetValue" [("par0","KA")] |> ignore
0
The similar code in C# works perfectly:
using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
namespace test_mysql_csharp
{
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello from test_mysql_csharp !");
var cs = "server=127.0.0.1;port=3306;database=test;Uid=***;Pwd=***;";
var sp = "GetValue";
var ps = new List<MySqlParameter>();
var p = new MySqlParameter("par0", "KA");
ps.Add(p);
var b = ExecStoredProc(cs, sp, ps.ToArray());
}
private static System.Data.DataTable ExecStoredProc(string cs, string spName,
params MySqlParameter[] spParams)
{
using MySqlConnection connection = new MySqlConnection(cs);
using MySqlCommand command = new MySqlCommand(spName, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(spParams);
connection.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
return dt;
}
}
}
A few lines to recreate test database and table:
CREATE SCHEMA `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci ;
use test;
CREATE TABLE `test`.`test` (
`k` VARCHAR(64) NOT NULL,
`v` VARCHAR(64) NULL,
PRIMARY KEY (`k`));
INSERT INTO test.test (k,v) values ('KA','VA');
DELIMITER $$
CREATE PROCEDURE `GetValue`(par0 varchar(64))
BEGIN
select * from test.test where k = par0;
END$$
DELIMITER ;
Thank you in advance -
Simple: What does THIS do?
command.CommandType = CommandType.StoredProcedure |> ignore
It just compared 2 values.
Should be:
command.CommandType <- CommandType.StoredProcedure |> ignore

How to change object value in a loop in Dart

I'm a total newbie in Dart and I've a lot of issues trying to change a member value of an Object inside a loop.
I've my object so defined:
class Cell {
int magicnum, x, y;
Cell(this.magicnum);
toString() {
return ("$magicnum - [$x][$y]");
}
}
I create a List of List of Cell (2d array) and then I need to fill x and y value according to position of each object in array.
for (int x = 0; x < DIM; x++) {
for (int y = 0; y < DIM; y++) {
grids[x][y].x = x;
grids[x][y].y = y;
}
}
This obviosly doesn't work because in Dart everything (also integer) is an Object and so, all Cell objects in my array have the same x and y value (they all got a reference to the same object).
How can I do?
Thanks
#julemand101
Array is made this way:
List<Cell> cells = List<Cell>.generate(DIM, (i) => Cell(i + 1));
List<List<Cell>> grids = List<List<Cell>>();
for (int i = 0; i < DIM; i++) {
grids.add(shuffleCell(cells));
}
You problem is you are not actually cloning each Cell object when you are doing the following (taken from the code example from comments):
List<Cell> newlist = List<Cell>.from(items);
Instead, you are creating a new List containing the same references to Cell objects as the previous list of items.
To create a copy of Cell objects you need to implement a clone method like:
class Cell {
int magicnum, x, y;
Cell(this.magicnum);
Cell.from(Cell cell)
: magicnum = cell.magicnum,
x = cell.x,
y = cell.y;
}
And do the following to iterate each element of the old list, create a new Cell object for each element and convert the result to a new List:
List<Cell> newlist = items.map((item) => Cell.from(item)).toList();

Building a DspComplex ROM in Chisel

I'm attempting to build a ROM-based Window function using DSPComplex and FixedPoint types, but seem to keep running into the following error:
chisel3.core.Binding$ExpectedHardwareException: vec element 'dsptools.numbers.DspComplex#32' must be hardware, not a bare Chisel type
The source code for my attempt at this looks like the following:
class TaylorWindow(len: Int, window: Seq[FixedPoint]) extends Module {
val io = IO(new Bundle {
val d_valid_in = Input(Bool())
val sample = Input(DspComplex(FixedPoint(16.W, 8.BP), FixedPoint(16.W, 8.BP)))
val windowed_sample = Output(DspComplex(FixedPoint(24.W, 8.BP), FixedPoint(24.W, 8.BP)))
val d_valid_out = Output(Bool())
})
val win_coeff = Vec(window.map(x=>DspComplex(x, FixedPoint(0, 16.W, 8.BP))).toSeq) // ROM storing our coefficients.
io.d_valid_out := io.d_valid_in
val counter = Reg(UInt(10.W))
// Implicit reset
io.windowed_sample:= io.sample * win_coeff(counter)
when(io.d_valid_in) {
counter := counter + 1.U
}
}
println(getVerilog(new TaylorWindow(1024, fp_seq)))
I'm actually reading the coefficients in from a file (this particular window has a complex generation function that I'm doing in Python elsewhere) with the following sequence of steps
val filename = "../generated/taylor_coeffs"
val coeff_file = Source.fromFile(filename).getLines
val double_coeffs = coeff_file.map(x => x.toDouble)
val fp_coeffs = double_coeffs.map(x => FixedPoint.fromDouble(x, 16.W, 8.BP))
val fp_seq = fp_coeffs.toSeq
Does this mean the DSPComplex type isn't able to be translated to Verilog?
Commenting out the win_coeff line seems to make the whole thing generate (but clearly doesn't do what I want it to do)
I think you should try using
val win_coeff = VecInit(window.map(x=>DspComplex.wire(x, FixedPoint.fromDouble(0.0, 16.W, 8.BP))).toSeq) // ROM storing our coefficients.
which will create hardware values like you want. The Vec just creates a Vec of the type specfied

Lua script for conky runs without errors but doesn't draw anything

I am new to lua and was trying to get more into it by creating scripts for conky. In my example, I was trying to encapsulate cairo functionality into a Canvas object and drawable objects (i.e. Text object) that can be added to the canvas.
When I tried to store cairo_surface and cairo objects in a table I wasn't able to use them anymore. Even though no error occured (no message or segfault or leaks) no text was displayed in the second example.
This example works:
Canvas = {
init = function (w)
local cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height)
local cr = cairo_create(cs)
return cr, cs
end,
destroy = function (cr, cs)
cairo_destroy(cr)
cairo_surface_destroy(cs)
end
}
function conky_main ()
if conky_window == nil then
return
else
local cr, cs = Canvas.init(conky_window)
local tx = Text:new{text="Hello World!"}
tx:draw(cr)
Canvas.destroy(cr, cs)
end
end
This example doesn't work:
Canvas = {
init = function (w) -- returns table instead of 2 variables
return {
cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height),
cr = cairo_create(cs)
}
end,
destroy = function (cnv)
cairo_destroy(cnv.cr)
cairo_surface_destroy(cnv.cs)
end
}
function conky_main ()
if conky_window == nil then
return
else
local cnv = Canvas.init(conky_window)
local tx = Text:new{text="Hello World!"}
tx:draw(cnv.cr) -- access table member instead of variable
Canvas.destroy(cnv)
end
end
return {
cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height),
cr = cairo_create(cs)
}
In Lua table constructor there is no way to access another fields of the table being constructed.
cs in expression cr = cairo_create(cs) refers to (global) variable cs instead of table field cs.
Workaround: introduce local variable cs and initialize it prior to creating a table.
local cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height)
return { cs = cs, cr = cairo_create(cs) }

Love2D Lua no idea why this doesn't work

Apologies for the unhelpful title but I really don't know what to call this. Anyway, I can't tell why this works:
local entity = require "entity"
entity:new(5,10,15,6)
local test = entity
print(test.x,test.y)
...but this doesn't...
local entity = require "entity"
local test = entity:new(5,10,15,6)
print(test.x,test.y)
Entity.lua simple contains:
local Entity = {}
function Entity:new(x,y,w,h)
self.x = x
self.y = y
self.width = w
self.height = h
end
return Entity
Case 1:
variable entity gets table which is returned from Entity.lua.
When you call Entity:new() in Entity.lua all the variable initialization is performed on table(object) entity. So, entity has variables x, y, width and height. You assigned table to test and printed it.
It works.
Case 2:
local test = Entity:new().
Here variable test takes return value of method new(), which is nil in this case, because function doesn't return any value.
It prints an error because table test doesn't have any keys called x and y.
If you want to create a new table with x, y, w, h you can do :
function Entity.new(x,y,w,h)
local newEntity = {}
newEntity.x = x
newEntity.y = y
newEntity.width = w
newEntity.height = h
return newEntity
end
or (but less readable) :
function Entity.new(x,y,w,h)
return {x = x, y = y, width = w, height = h}
end

Resources