Load a point layer from CSV in PyQGIS QGIS 3.10 - csv

I'm trying to use a script that loads in a point layer from CSV. The code that I used is:
uri = "file:///some_path/PyQGIS/Some.csv?delimiter={}&xField={}&yField={}".format(os.getcwd(), ";", "X", "Y")
vlayer = QgsVectorLayer(uri, "Name", "delimitedtext")
QgsProject.instance().addMapLayer(vlayer)
The structure of the .csv file is as follows:
SomeValue ----- X ----- Y
Hereby, the coordinates are put according to Amersfoort (EPSG:4289) e.g.: X = 213404, Y = 500846. When trying to load this in manually, everything is going fine. However in the script, it does not seem to work. To clarify: The delimiter used is a ;
Thanks in advance.

You can specify the projection of your data set by adding &crs=epsg:4723 to the URI definition. So your case becomes:
uri = "file:///some_path/PyQGIS/Some.csv?delimiter={}&crs=epsg:4289&xField={}&yField={}".format(os.getcwd(), ";", "X", "Y")
vlayer = QgsVectorLayer(uri, "Name", "delimitedtext")
QgsProject.instance().addMapLayer(vlayer)

The answer to this is issue has been solved by removing the "os.getcwd()" part of the code. Hope this will help anyone with similar issues.

Related

Trying to define a function that creates lists from files and uses random.choices to choose an element from the weighted lists

I'm trying to define a function that will create lists from multiple text files and print a random element from one of the weighted lists. I've managed to get the function to work with random.choice for a single list.
enter code here
def test_rollitems():
my_commons = open('common.txt')
all_common_lines = my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
y = random.choice(common)
print(y)
When I tried adding a second list to the function it wouldn't work and my program just closes when the function is called.
enter code here
def Improved_rollitem():
#create the lists from the files#
my_commons = open('common.txt')
all_common_lines= my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
my_uncommons = open('uncommon.txt')
all_uncommon_lines =my_uncommons.readlines()
uncommon =[]
for i in all_uncommon_lines:
uncommon.apend(i)
y = random.choices([common,uncommon], [80,20])
print(y)
Can anyone offer any insight into what I'm doing wrong or missing ?
Nevermind. I figured this out on my own! Was having issues with Geany so I installed Pycharm and was able to work through the issue. Correct code is:
enter code here
def Improved_rollitem():
#create the lists from the files#
my_commons = open('common.txt')
all_common_lines= my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
my_uncommons = open('uncommon.txt')
all_uncommon_lines =my_uncommons.readlines()
uncommon =[]
for i in all_uncommon_lines:
uncommon.append(i)
y = random.choices([common,uncommon], [.8,.20])
if y == [common]:
for i in [common]:
print(random.choice(i))
if y == [uncommon]:
for i in [uncommon]:
print(random.choice(i))
If there's a better way to do something like this, it would certainly be cool to know though.

Set data in UNB segment EDIFACT

Do you know how I can set UNB in EDIFACT?
I have a CSV input file:
VRD1;100;200;0;0;L
And the output should be:
UNA:+.? '
UNB+UNOA:2+100:14+200:14+200305:0704+00000000000000++AAA'
UNH+1+INVRPT:D:96A:UN:EAN005'
BGM+10::9+20200305070403+9
I have to map the second and third fields to UNB.
I appreciate any help in this regard. Thank you in advance.
You should be able to do this using the EdiOverride message context properties. You can find them in the Microsoft.BizTalk.Edi.BaseArtifacts assembly found in the BizTalk Server installation folder.
For instance, in an orchestration, you can do this:
EdifactMessage(EdiOverride.OverrideEDIHeader) = true;
EdifactMessage(EdiOverride.UNB2_1) = xpath(CsvMessage, ...); // 100
EdifactMessage(EdiOverride.UNB3_1) = xpath(CsvMessage, ...); // 200
Add a correlation set with the same properties to your send shape.
If you want to set them in a pipeline, use the property namespace http://schemas.microsoft.com/BizTalk/2006/edi-properties.

How do I append text after an answer in Octave?

I am new to Octave and I've written the following function to combine sound pressure levels. I want to present the answer with a reference value after it. e.g.: 83 dB re: 20µPa rather than just 83, but I don't know how to add the text after the answer.
function L_total = combineLevels(Lp)
x = Lp./10;
y = 10.^x; z = sum(y);
L_total = 10*log10(z);
endfunction
Any advice would be appreciated. Thanks!
Sounds like you want to use fprintf or sprintf. These functions allow you to construct strings that interpolate values from variables. Try:
fprintf('%d dB re: %duPa\n', L_total, Lp);

Storing matlab array in MySQL. Again

I have a 3D array in Matlab of uint16(basically it is just an image 1080x1920x3). I want to store it in mysql. Here is what I'm doing:
MySQL:
create table imgtest(img longblob);
Matlab:
% image_data - is my image as described before
raw_im = reshape(image_data,1,[]);
conn = database('test','root','root','Vendor','MySQL','Server','localhost')
x = conn.Handle;
insertcommand = ['INSERT INTO imtest (img) values (?)'];
StatementObject = x.prepareStatement(insertcommand);
StatementObject.setObject(1,raw_im)
StatementObject.execute
The problem is that I'm writing about 600k uint16 values into this blob field. But when I take this field from the DB, I always getting about 1.2 million of uint8 elements(exactly two times more).
So, is there a way to read this byte field as a set of uint16, but not uint8?
Thank you.
I have been doing something similar for one of my projects
basically there was one difference but maybe it would clarify something to you.
I was loading image directly to DB from file with command:
INSERT INTO BaseImage(Image)
SELECT * FROM OPENROWSET(BULK N'C:\co.jpg', SINGLE_BLOB) as image
and getting it back to Matlab required typecasting (just like #sebastian mentioned)
SQL_query = 'select TOP 1 pk_BaseImage,Image from BaseImage order by pk_BaseImage desc';
[data] = SQL_query_exec(SQL_query);
pk_BaseImage = data.Data.pk_BaseImage;
out = typecast(data.Data.Image{1,1},'uint8');
BUT..
it was not enough, I had to do some trick to use 'out' as image
I was forced to write it to temporary file and read it again to Matlab (I know it's strange but it worked very well and I could for example calculate DWT, DFT and so on)
image_matrix = get_image_matrix( out );
get_image_matrix function looks like:
function [ out ] = get_image_matrix( input )
targetfilename = 'temp.jpg';
%wynik
fid = fopen(targetfilename,'w');
if fid
fwrite(fid,input,'uint8');
end
fclose(fid);
out = imread(targetfilename);
delete(targetfilename);
end
I hope it will help you :)
One important notice - I used gray-scale images (uint8 type)
You can most probably typecast the uint8's into uint16's to get back at your original image data:
uint16_result = typecast(uint8_result, 'uint16');
I'm not familiar with the database toolbox - so there might well be a way to tell Matlab to do this on its own.
OK, thank you both. I've summarized your answers and this what I've got:
Since blob field is nothing more than byte array, then we should cast our data in matlab before writing it to the DB. After reading it from DB, we should cast them back.
Minimum working example is:
MySQL
create table imgtest(img longblob);
Matlab
% image_data - is my image as described before
raw_im = typecast(reshape(image_data,1,[]),'uint8'); %! the main string
conn = database('test','root','root','Vendor','MySQL','Server','localhost')
x = conn.Handle;
insertcommand = ['INSERT INTO imtest (img) values (?)'];
StatementObject = x.prepareStatement(insertcommand);
StatementObject.setObject(1,raw_im)
StatementObject.execute
After we can read it back:
res = exec(conn,'Select * from imtest')
array_uint8 = fetch(res);
array_uint8 = array_uint8{1};
array_uint16 = typecast(array_uint8,'uint16').
Hope this will help someone.

AS3: Getting values of objects by referencing their name

I'm reading XML and attaching values to objects in two seperate movieclips. Like this
Map01:
Marker01.name = hello there
Marker01.short = hel
Marker01.value = 12
Map02:
Marker02.name = hello there
Marker02.short = hel
Marker02.value = 99
Now I'm clicking on Marker01 in Map01 and get its name and value. I want to compare its value to that of Marker01 in Map02, using the name, or better yet .short because the names are long and use special characters/spaces. How do I do this? I've pretty much tried everything that seemed logical!
EDIT: sample code for clarification
var marker01:mc_marker = new mc_marker();
marker01.name="hello there";
marker01.short="abc";
marker01.val="99";
marker01.x=10;
marker01.y=10;
this.mc_map01.addChild(marker01);
var marker02:mc_marker = new mc_marker();
marker02.name="hello there";
marker02.short="abc";
marker02.val="20";
marker02.x=10;
marker02.y=10;
this.mc_map02.addChild(marker02);
marker01.addEventListener(MouseEvent.MOUSE_UP, showMarkerInfo);
marker02.addEventListener(MouseEvent.MOUSE_UP, showMarkerInfo);
function showMarkerInfo(event:MouseEvent):void {
txt_ms.text=event.target.short;
txt_mv.text=event.target.val;
if (event.target.short==mc_map02.marker02.short){
txt_mvi.text="here should be the marker02 value";
}
}
You have a typo there. Map02 use Marker1 things.
If its a typo in Stackoverflow,
this.getChildByName( "Marker01" ) will return you the movieclip, buy its name. take care though, as "name" is what it searches for. You used "hello there" when you should put Marker01 as the name. I would suggest you put a property called "data" and put the xml info in it so it doesn't conflict.
In the end you have:
if( this.getChildByName( "Marker01" ).data.value == this.getChildByName( "Marker02" ).data.value ).
I assume this is because you generate Marker0X at runtime and you can't declare some variables and use them directly.
Browny points if you make "data" a instance of a custom class where you can compare two "data". If you need more help, add a comment ^_^