Saturday, November 18, 2017

JSON to Dictionary Conversion


Here is the tip to convert JSON Object to Dictionary.


private static Dictionary<string, object> GetDictFromJObject(JObject inJObject)
{
        if (inJObject == null) return new Dictionary<string, object>();
        return ((JObject)inJObject).ToObject<Dictionary<string, object>>();
}

JavaScript Function to get a Valid Number from String


Here is the function to get a numeric value from a string value.


    function GetValidNumberValue(inValue)
   {
        if (inValue == null) return 0.0;
        if (inValue == undefined) return 0.0;
        if (isNaN(inValue) == true) return 0.0;
        let togoNumber = parseFloat(inValue);
        if (isNaN(togoNumber) == true) return 0.0;
        return togoNumber;
    }

JavaScript functions to handle cookies


Here are the functions to handle cookies using JavaScript.

    function SetCookie(cookieName, cookieValue, exdays) {
        let d = new Date();
       d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        let expires = "expires=" + d.toUTCString();
        document.cookie = cookieName+ "=" + cookieValue+ "; " + expires;
    }

    function GetAllCookie() {
        return document.cookie;
    }

    function DelCookie(cname) {
        let name = cname + "=";
        document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
        return "";
    }

    function GetCookie(cname) {
        let name = cname + "=";
        let ca = document.cookie.split(';');
        for (let i = 0; i < ca.length; i++) {
           let c = ca[i];
           while (c.charAt(0) == ' ') c = c.substring(1);
           if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }


JavaScript Function to get the website URL Value


This JavaScript function will return the URL name along with the protocol ('http' or 'https') value.

function GetHost() {

        if (typeof location.protocol == 'undefined') {

            alert('location.protocol is undefined');

        }

        if (typeof location.host == 'undefined') {

            alert('location.protocol is undefined');

        }

        let host = location.protocol + '//' + location.host + '/';

        return host;

    }

Get JSON string from XML

Here is the function to convert XML string to JSON string.

string GetJSONStringFromXML(string inXMLString){
XDocument xDoc = XDocument.Parse(inXMLString); //orXDocument.Load(xmlfilepath)
string jsonString = JsonConvert.SerializeXNode(xDoc);
return jsonString;
}

Sunday, February 12, 2017

Alternate approach to build Geographic Information Systems (GIS) Applications


GIS based Web Applications are very powerful and used extensively to make significant business decisions. Building a GIS based Web Application can be a challenge as it involves many client and server side technologies. In recent days, open source GIS has become very popular. Since some shops have already invested in Microsoft SQL Server, this can be used effectively for building powerful GIS solutions along with open source GIS tools.
Microsoft SQL Server supports various spatial functions. Detailed documentation can be found here.
SQL Server supports two spatial data types: the geometry data type and the geography data type.
  • The geometry type represents data in a Euclidean (flat) coordinate system.
  • The geography type represents data in a round-earth coordinate system.
Pick the right type based on your business needs. Write simple C# code to manipulate the data stored in SQL Server. You can export the stored SQL spatial data to GeoJSON files using tools like GeoJSON.NET. You can also use QGIS to connect directly to SQL Server and export the data to GeoJSON format. At the end we need GeoJSON format files which can be loaded and viewed in Google Maps using Google Maps JavaScript API.




The SRID (Spatial Reference System Identifier) value 4326 works well all across from SQL Server to Google Maps. For more details check here. In SQL Server Spatial, there are functions to check envelope angle and reorient the polygon if necessary.
Google Maps JavaScript API will allow you to load GeoJSON files and details can be found here. Google Maps JavaScript API also has an excellent Geometry Library which has several functions like a  given point falls inside a polygon or on the edge, etc.,
To summarize, a powerful GIS Web application can be built using SQL Server, QGIS, and Google Maps API.

Monday, January 9, 2017

Logging in DotNetCore Application

To view any exception raised in the dotnetcore application, these are the steps to do.

Step 1:

First make sure the “web.config” has “stdoutLogEnabled” enabled as “true” and the “stdoutLogFile” path is set correct. Use as shown below. Do not end the path with “\”. The reason is, the application will use the last portion of the name as file prefix along with the timestamp. For example in this case the log file name is something like “stdoutLog_4036_201716232359.log” and this file will be present inside the folder “C:\inetpub\wwwroot\ProjectTest\Logs\”.
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile="C:\inetpub\wwwroot\ProjectTest\Logs\stdoutLog" forwardWindowsAuthToken="false"/>

Step 2:
Make sure to give write permission to the log folder (in my example ““C:\inetpub\wwwroot\ProjectTest\Logs\”) for the IIS_IUSRS.
 These two steps will ensure logging is happening when an exception is raised in the code.
 I added a “TEST EXCEPTION” in the code like this.
         public void ConfigureServices(IServiceCollection services)       {
            // Add framework services.
            services.AddMvc();
             services.AddTransient<IBizAssign, BizAssign>();
             throw new Exception("TEST EXCEPTION");
        }

This was logged in the log file as per the expectation.
The same log file can be used for non-exception general logging.
Just the follow this article to setup logging. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging

Simply start logging wherever necessary like shown below.

            logger.LogInformation("Get function entered.");

This will write all the information in the log file.

All Blogs so far ...