Search This Blog

Thursday, September 13, 2012

C# Parallel Convert a List<objects> to String with Custom Formatting

 

I wrote some code a while ago which did some processing, creating a list output, which I then had to turn into a String to TCP send it to a remote client.

It worked but took a while...

I was wondering why.... turns out the processing is quick as in parallel.. but the bottle neck was in the sending part...

Step 1... add a method to zip up the string and send that = slight performance increase as now sending a lot less data

Step 2... parallelise the list to string part...

It was like this.. and with about 40,000 items (egogridviewresult objects) in the list.. it'd take 20 or so seconds

for each (egogridviewresults v in r)
{
res += v.tarpt.X.ToString("0.00")+","+v.tarpt.Y.ToString("0.00")+","+v.cellx + "," + v.celly + "," + v.objid + "," + v.distance + "#";
}
res = Zip(res);
sendToClient(res);

- - - -  

I've now changed to this and it does the same job in about 2 seconds!

var results = new ConcurrentQueue<string>();

Parallel.ForEach(r, v =>
{
String r1 = v.tarpt.X.ToString("0.00") + "," + v.tarpt.Y.ToString("0.00") + "," + v.cellx + "," + v.celly + "," + v.objid + "," + v.distance.ToString("0.00");
results.Enqueue(r1);
});

res = String.Join("#", results);
res = Zip(res);
sendToClient(res);

__________________

Things learnt...

1) C# .NET 4 has a cool function String.Join which can concatenate a list into a string with a defined delimiter of your choice! 

2) Parallel.ForEach is just awesome!

Thursday, September 8, 2011

Raster access speeds… and size limits

 

I need to be able to handle large raster images, and to be able to find values from cells very quickly.

There are many ways to do this…. here’s a look at a few:

Method Handles GeoCoords Max Size Read Access Speed
DOTSPATIAL
Raster
Yes Medium (9k x 9k) 2.6 million per second
C# dictionary No Small (5k x 5k) 50 million per second
EMGU IMGs No Large (30k x 30k) 3.2 million per second
C# BMP (.net) No Medium (9k x 9k) 1 million per second
C# FastBMP No Medium (9k x 9k) 33 million per second
Rasterlite Yes Large (internal tiling) (to do)
GDAL Yes Large (to do)
R (grids) (to do) (to do) (to do)
XNA texture No Small   (4k x 4k) (to do)

Tuesday, September 6, 2011

DotSpatial

So I’m checking out DotSpatial for use in a project.. and the first thing on my list is to check it can convert coordinate systems easily from WGS84 <> OSGB36.

Seems the current version’s inbuilt definition of OSGB36 isn’t correct.. and when using it with the Reproject method gives out some strange results. It’s also listed as failing the unit test currently.

However you can specify the details yourself.. and then it seems to work just fine… so here’s the code for taking a coordinate as OSGB36 and transforming it to WGS84…

PointD pd = new PointD();
pd.X = 326097.07;
pd.Y = 673630.932;

double [] pts_xy = new double[2];
pts_xy[0] = pd.X;
pts_xy[1] = pd.Y;

double [] pts_z = new double[1];
pts_z[0] = 0;

var pStart = new ProjectionInfo("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 +units=m +no_defs");
var pEnd = KnownCoordinateSystems.Geographic.World.WGS1984;
 
Reproject.ReprojectPoints(pts_xy, pts_z, pStart, pEnd, 0, 1);
System.Console.WriteLine(pts_xy[0] + " " + pts_xy[1]);

Thursday, August 18, 2011

WGS84 to OSGB36

A transformation between WGS84 and OSBB36 requires a datum transformation. A 7 parameter transformation should give results accurate to about +/- 5m…

ArcGIS supports a number of 3 para, and 7 para transforms. CS2CS using Proj may also be used as follows :

cs2cs +proj=latlong +ellps=WGS84 +towgs84=0,0,0 +nodefs +to +proj=tmerc
+lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy
+towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 +units=m
+no_defs

(thanks to Paul from http://osgeo-org.1803224.n2.nabble.com/OSGB36-td2062542.html for settings)

However for a centimetre accurate transformation an NTv2 grid is required. The OS provide an online coordinate transformation service here (a site for bulk conversions is also available)

http://gps.ordnancesurvey.co.uk/etrs89geo_natgrid.asp

It’s also possible to download the grid in NTv2 format from here:

http://www.ordnancesurvey.co.uk/oswebsite/gps/osnetfreeservices/furtherinfo/ostn02_ntv2.html

or directly from this link http://www.ordnancesurvey.co.uk/oswebsite/gps/docs/OSTN02_NTv2_DataFiles.zip

Test for a point in Edinburgh (Scotland)

WGS84 –3.185, 55.95
OS Online NTv2 Conversion > 326097.07,673630.932 [taken to be correct conversion]
ESRI (3 par option_1) > 326097.86,673625.357
ESRI (3 par option_5) > 326095.042,673619.452
ESRI(petroleum option)> 326096.974,673630.405
CS2CS (not using NTv2)> 326096.98,673630.4

So it would appear that ESRI’s petroleum option and CS2CS (as above without NTv2) and the OS Online conversion tool all agree to within 1 metre, while the 3 parameter version are out by at least 5m. The options relate to which parts of the country the re-projection has been set up for… this test would suggest option_1 works better for Scotland.

Friday, August 12, 2011

SpaceBook Project

So we’ve arrived in Edinburgh and I’ve started working with the SB team… William’s away and kindly allowing me to use his office for the moment.

Useful Data sources (thanks to Guy):

MasterMap – building outlines from OS (downloaded thru EDINA)
LiDAR and 3D building (from DSM-DTM) polygons from http://landmap.ac.uk/

edin 3d from landmap

(The 3D buildings from Landmap SHP source)

 



GRASS – new viewshed function (ie not r.los)

r.viewshed (addon) – using faster algo (sweep method)

Install method for Windows summary – download GRASS source (all).. add r.viewshed source folders… change the MAKEFILE to incl r.viewshed – compile GRASS as per this page:


http://trac.osgeo.org/grass/wiki/CompileOnWindows

Using msys

/mswindows/osgeo4w/package.sh
the package error log goes into this folder:
\\OSGeo4W\usr\src\grass641\mswindows\osgeo4w as package.log

Thursday, June 23, 2011

Manually signing an Android App

1) Open CMD and then go to c:\Program Files\Java\jdk1.6.0_25\bin

2) Setting up the keystore
keytool -genkey -v -keystore c:\android\mks -alias myapp123 -keyalg RSA –keysize 2048 -validity 10000
enter a password (and again)
enter your name details (optional)
enter y to keep and progress…
re-renter your password

3) Export unsigned APK from Eclipse then sign it…
Export by Right Click > Android Tools
jarsigner -verbose -keystore c:\android\mks d:\myapp.apk myapp123

4) check it’s signed OK by using jarsigner verify
jarsigner –verify –verbose d:\myapp.apk

5) Zip align your code…
Go to Andriod-sdk-windows-tools

(It always uses an int value of 4 for byte ordering - this isn't API level etc)

zipalign -v 4 your_project_name-unaligned.apk   your_project_name.apk

eg zipalign -v 4 d:\GeoSensorLogger.apk  d:\GeoSensorLogger_signed.apk

Put the apk file on your phone.. or marketplace…

Tuesday, May 24, 2011