Leaflet and maps

if you want to display data on maps on the web, then you have a solution for javascript: Leaflet.

First , download the css and js for leaflet from http://leafletjs.com/ . ( and also some plugins, such as lealflet label)  You can include like this:

<link href="~/Scripts/leaflet/leaflet.css" rel="stylesheet"/>
<link href="~/Scripts/leafletlabel/leaflet.label.css" rel="stylesheet"/>

<script src="~/Scripts/leaflet/leaflet.js"></script>

<script src="~/Scripts/leafletlabel/leaflet.label.js"></script>

Then you must obtain the data for the map as GeoJson  – for example, from http://www.geo-spatial.org/file_download/29413 you can obtain data for Romania counties. ( If your country is, for example , USA , you can follow this link : http://lmgtfy.com/?q=usa+geojson )

Define a javascript variable (varMyCounties) = your json.

$( document ).ready(function() {
            map = L.map(‘map’).setView([46,25], 7); //here put your country coordinates

            L.tileLayer(‘https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw’, {
                maxZoom: 7,
                minZoom: 7,
                attribution: ‘Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ‘ +
                    ‘<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ‘ +
                    ‘Imagery © <a href="http://mapbox.com">Mapbox</a>’,
                id: ‘mapbox.light’
            }).addTo(map);

            L.geoJson(varMyCounties, {
                onEachFeature: onEachFeature
            });

 

//customize the display

function onEachFeature(feature, layer) {
            if (feature.geometry && feature.geometry.type)
                if (feature.geometry.type === ‘Polygon’) {

                    // Get bounds of polygon
                    var bounds = layer.getBounds();
                    // Get center of bounds
                    var center = bounds.getCenter();
                    // Use center to put marker on map

                    var countyName = feature.properties.NAME;

//here put a circle on map, depending on the countyName
  radCircle = (40 – 10) * val + (10 * @max – 40 * @min);
                    radCircle = radCircle / (@max – @min);
                    var circle = L.circleMarker([center.lat, center.lng], { fillColor: "#f00", radius: radCircle })
                        .bindLabel(feature.properties.NAME_UTF8 +’:’+ val, { direction: ‘left’})
                       
                        .addTo(map);

                }
        }

 

 

You can see an example at http://ropwebmvc20160213092204.azurewebsites.net/Home/ShowMap/1AEA07DB-4F14-42DC-989A-91241A020182

Swashbuckle.MVC

 

The Swashbuckle package for documenting WebAPI is really awesome. I have used for documenting Exporter API for Word/Excel/PDF/ODS/ODT.

One problem though: It does not respect the layout of your site.

For example, if the original header is this

 

when  I click on API test(swagger)

 

. This is not what I want – since I want the same layout.

Enter NuGet package https://www.nuget.org/packages/Swashbuckle.MVC/ . What it does is

1. add a controller(SwashbuckleMVCController), and Index action inside the controller.

2. add a View(SwashbuckleMVC/Index.cshtml) to your application

3. add a module to your application . The module intercept /swagger/ui/index call and modify with a Filter the send data ( practically, it adds to the beginning and the end of the stream the bytes from the Index action that I mentioned at 1)

Cons: It adds another <html> inside the main <html> . I made an issue on Swashbuckle to mention that https://github.com/domaindrivendev/Swashbuckle/issues/658

 

The code is free on https://github.com/ignatandrei/Swashbuckle.MVC .

You can test it on http://exporter.azurewebsites.net/swagger/ui/index

RavenDB embedabble

I have made an application with RavenDB embeddable.

The good part is that is embeddable and easy to work with – at least, at start.

What I have learned:

  • If you forgot to use using, then RavenDB will throw an exception complaining about multiple sessions. Code:
 using (var session = instanceDefault.OpenSession(DatabaseName()))
{

//code
}

 

  • If you use multiple instance of the same EmbeddableDocumentStore , even on different default databases , then will complain about “temp path already used by another database instance “ . And the generics do NOT share the same instance of a static variable. And when you dispose a static singleton instance ? At the final of the application ( do not forget , otherwise you may have again “temp path already used by another database instance ” !)

 

  • The most simple index for new databases is RavenDocumentsByEntityName . Code:
//static EmbeddableDocumentStore instanceDefault;
var dbc = instanceDefault.DatabaseCommands.ForDatabase(DatabaseName());
                var defIndex = new RavenDocumentsByEntityName();
                
                defIndex.Execute(dbc,new DocumentConvention()
                {
                    DefaultUseOptimisticConcurrency = true                    
                    
                } );

 

  • If you create an index, create in the Database that needs. Otherwise if will complain when you get the data.  ( see previous code)

 

  • To manage data / databases , there is RavenDB Management Studio – a fancy name for an application that you can access ( by default ) at http://localhost:8080 . Code:
            instanceDefault = new EmbeddableDocumentStore();
//I have put this in app.config/web.config
            //instanceDefault.Configuration.Settings.Add("Raven/MaxSecondsForTaskToWaitForDatabaseToLoad", "60");
/*
<connectionStrings>
		<add name="RavenDB" connectionString="DataDir = ~\App_Data\Database"/>
	</connectionStrings>
   <appSettings>
	<add key="Raven/MaxSecondsForTaskToWaitForDatabaseToLoad" value="60"/>
*/
            instanceDefault.ConnectionStringName = "RavenDB";
            instanceDefault.Conventions.FindIdentityPropertyNameFromEntityName = (entity) => "ID";

            try
            {
                //To try in debug mode
                NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);
                instanceDefault.UseEmbeddedHttpServer = true;
                instanceDefault.Initialize();

            }
            catch (ReflectionTypeLoadException ex)
            {
                string message = "LoaderExceptions:";
                ex.LoaderExceptions.ForEach(it => message += it.Message + ";");
                throw new Exception(message, ex);
            }


 

( do not let this into Web Application if you want to deploy to a third party host)

 

  • Copy / paste database do not work. You must backup and restore data( preferably with RavenDB Management Studio )

 

  • RavenDB documentation is ok – however, embeddable database is not so represented very well

 

If you want to see a sample ( not very good, though) repository, I have created one at https://github.com/ignatandrei/RomaniaOpenData/blob/master/ROP/ROPInfrastructure/Repository.cs

Bottom line: I think that is good enough.

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.