C#でSVGを表示

C#のFORMに手っ取り早くSVGを表示する。

SVG.NETをダウンロード。
2019/10/30現在 コンパイルするには.NET Framework 4.5.2が必要!
今回はVisualStudio2013で使いたいので探してみたら
SVG-master\Samples\RuntimeUpdates\LibにSvg.dllがあったのでこれを参照設定する

Form上にPictureBoxを置いて、SizeModeをStretchImageに設定。

ボタンクリックイベントに以下のソースを貼り付け

using svg;

var svgDoc = SvgDocument.Open(@"C:\3Dkaoku\svg\circle.svg");
if (pictureBox1.Image != null)
    pictureBox1.Image.Dispose();
pictureBox1.Image = svgDoc.Draw();
<img alt='' class='alignnone size-full wp-image-1628 ' src='http://www.yasumite.com/blog/wp-content/uploads/2019/10/img_5db92420b2770.png' />
カテゴリー: C#

MapBox SDK を iOS Objective-C から使用

最初にmapboxのサイトでアカウントを作成しログイン

MapBox SDKのページを開く

https://docs.mapbox.com/ios/maps/overview/

インストールボタンをクリック


ダウンロードボタンをクリック


mapbox-ios-sdk-5.4.0-dynamic.zipをダウンロード

XCODEを起動してSingle View Appのプロジェクトを作成

ダウンロードしたmapbox-ios-sdk-5.4.0-dynamic.zipを展開し中のMapbox.frameworkをEmbedded Binariesにドラッグして参照を追加

TARGETSのBuild Phases画面を開き、+(追加)ボタンを押す

Run Scriptを追加しMapboxのサイトに従い設定

info.listにMGLMapboxAccessTokenを追加しキーを入力


ViewController.mを編集

#import "ViewController.h"
@import Mapbox;


@interface ViewController () <MGLMapViewDelegate>
@property (nonatomic) MGLMapView *mapView;
@end

地図を表示

ViewControllerのviewDidLoadを以下に従って編集

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"mapbox://styles/mapbox/streets-v11"];
    self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:url];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(35.73353,139.712118)
                       zoomLevel:12
                        animated:NO];
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(offlinePackProgressDidChange:) name:MGLOfflinePackProgressChangedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(offlinePackDidReceiveError:) name:MGLOfflinePackErrorNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(offlinePackDidReceiveMaximumAllowedMapboxTiles:) name:MGLOfflinePackMaximumMapboxTilesReachedNotification object:nil];
}

池袋を中心に地図を表示

デフォルトのAnnotationを表示

- (void)mapViewDidFinishLoadingMap:(MGLMapView *)mapView {
    MGLPointAnnotation *hello = [[MGLPointAnnotation alloc] init];
    hello.coordinate = CLLocationCoordinate2DMake(35.73353,139.712118);
    hello.title = @"カーネル";
    hello.subtitle = @"Welcome to my marker";
    [mapView addAnnotation:hello];
}

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation {
    return nil;
}

- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation {
    return NO;
}

関数:annotationCanShowCalloutの戻り値にYESを返すとAnnotationをクリックするとTooltipを表示する

デフォルトのAnnotationではなくMGLAnnotationViewを使ってカスタムAnnotationを表示

viewForAnnotationでnilではなく、MGLAnnotationViewを返す

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation {
    // 100はannotationを区別するキー。テストなので固定
    MGLAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"100"];
    if (!annotationView) {
        annotationView = [[MGLAnnotationView alloc] initWithReuseIdentifier:@"100"];
        annotationView.frame = CGRectMake(0, 0, 30, 30);
        annotationView.layer.cornerRadius = annotationView.frame.size.width / 2;
        annotationView.layer.borderColor = [UIColor whiteColor].CGColor;
        annotationView.layer.borderWidth = 4.0;
        annotationView.backgroundColor = [UIColor colorWithRed:0.03 green:0.80 blue:0.69 alpha:1.0];
    }
    
    return annotationView;
}

MGLAnnotationViewを選択するとdidSelectAnnotationViewイベントが発生する

- (void)mapView:(MGLMapView *)mapView didSelectAnnotationView:(MGLAnnotationView *)annotationView {
    NSLog(@"didSelectAnnotationView");
}

Annotationに画像を表示

viewForAnnotationではnilを返すように戻し、imageForAnnotationでMGLAnnotationImageを返すように変更

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation {
    return nil;
}

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
    
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"100"];
    if (!annotationImage) {
        UIImage *image = [UIImage imageNamed:@"place8.png"];
        image = [image imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, image.size.height/2, 0)];
        annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"100"];
    }
    
    return annotationImage;
}

背景を国土地理院のタイル(ラスタ)に変更

baseman.jsonを作成(ファイル名は任意)

このファイルの内容はmapbox-gl-native for ios で地理院地図を表示してみるのそのまんまです

{
  "version": 8,
  "name": "Raster Tiles",
  "sources": {
    "gsiStd": {
      "type": "raster",
      "tiles": [
          "http://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png"
      ],
      "tileSize": 256
    }
  },
  "layers": [{
    "id": "gsiStd",
    "type": "raster",
    "source": "gsiStd",
    "paint": {
      "raster-fade-duration": 100
    }
  }]
}

baseman.jsonをプロジェクトに追加

MGLMapViewのコンストラクタに渡すstyleURLを変更

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"basemap" withExtension:@"json"];
    self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:url];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(35.73353,139.712118)
                       zoomLevel:12
                        animated:NO];
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
    
    以下省略
}

githubにリポジトリを作成、ローカルリポジトリからPUSHまで

既にgithubの使い方は説明しているがSSHを使用しない方法を記載

PC上にはgit for windowsがインストール済であることを前提とする

github上でリポジトリを作成

PC上にローカルリポジトリのフォルダ「myrepo」を作成

カレントを「myrepo」に移動しローカルリポジトリを初期化

C:\projects\test\git>mkdir myrepo
C:\projects\test\git>cd myrepo
C:\projects\test\git\myrepo>git init
Initialized empty Git repository in C:/projects/test/git/myrepo/.git/
C:\projects\test\git\myrepo>

ファイルを作成して「add」、「commit」

C:\projects\test\git\myrepo>echo hello > readme.txt
C:\projects\test\git\myrepo>git add .
C:\projects\test\git\myrepo>git commit -m "comment"

github上からリポジトリのURLをコピー

リモートリポジトリを設定し、push
※githubのアカウントを聞かれたらメールアドレスとパスワードを入力

C:\projects\test\git\myrepo>git remote add origin https://github.com/省略/myrep.git
C:\projects\test\git\myrepo>git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/省略/myrep.git
 * [new branch]      master -> master

C:\projects\test\git\myrepo>

頂点エディタで編集時にスナップを有効にする

スナップツールバーでスナップのON/OFFを設定

以前、このスナップツールバーが有効にできずにユーザーグループで問い合わせをしたらレイヤとプロジェクトのEPSGが同じでないと有効にできないはずと回答をいただいた。確かにその時に確認した。

しかし改めて確認したところプロジェクトのEPSG=3857、レイヤのEPSG=4326の状態でスナップがONにできた。

Pythonでは以下のコードでスナップの有効、無効を設定可能

スナップを有効にする

“`
config = QgsProject.instance().snappingConfig()
config.setEnabled(True)
QgsProject.instance().setSnappingConfig(config)
“`

カテゴリー: QGIS

Feature登録時に属性情報入力ダイアログを表示しない

Featureを登録すると通常は属性情報入力ダイアログが表示される。

レイヤのプロパティでFeature登録時に属性情報入力ダイアログの表示/非表示を設定できる

Pythonでの設定方法

属性情報入力ダイアログを非表示

layer = self.iface.activeLayer()
editFormConfig = layer.editFormConfig()
editFormConfig.setSuppress(QgsEditFormConfig.FeatureFormSuppress.SuppressOn)
layer.setEditFormConfig(editFormConfig)

属性情報入力ダイアログを表示

layer = self.iface.activeLayer()
editFormConfig = layer.editFormConfig()
editFormConfig.setSuppress(QgsEditFormConfig.FeatureFormSuppress.SuppressOff)
layer.setEditFormConfig(editFormConfig)

線分と点の最短距離

points=[]
point = QgsPoint()
point.setX(10)
point.setY(10)
points.append(point)
point = QgsPoint()
point.setX(30)
point.setY(20)
points.append(point)
line = QgsLineString()
line.setPoints(points)

point = QgsPoint()
point.setX(15)
point.setY(10)

p = QgsGeometryUtils.closestPoint(line, point)

p
<QgsPoint: Point (14 12)>

iOSのMKMapViewでタイル表示

iOSのMKMapViewでApple Mapではなく他のタイルサーバー(OSM)を使用するにはMKMapViewのオーバーレイ機能を使用する。

MKMapView *mv = [[[MKMapView alloc]initWithFrame:rect]autorelease];
[mv setMapType:MKMapTypeStandard];
[mv setDelegate:self];
    
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
overlay.canReplaceMapContent = YES;
[mv addOverlay:overlay level:MKOverlayLevelAboveLabels];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
    }
    return nil;
}